利用 Intellij IDEA +MAVEN+Jetty实现SpringMVC读取数据库数据并显示在页面上的简单功能
1 新建maven项目,配置pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>SpringJDBCTest1</groupId> 5 <artifactId>SpringJDBCTest1</artifactId> 6 <packaging>war</packaging> 7 <version>1.0-SNAPSHOT</version> 8 <name>SpringJDBCTest1 Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 11 <dependencies> 12 13 <!--spring--> 14 <dependency> 15 <groupId>org.springframework</groupId> 16 <artifactId>spring-beans</artifactId> 17 <version>4.3.1.RELEASE</version> 18 </dependency> 19 20 <dependency> 21 <groupId>org.springframework</groupId> 22 <artifactId>spring-core</artifactId> 23 <version>4.3.1.RELEASE</version> 24 </dependency> 25 26 <dependency> 27 <groupId>org.springframework</groupId> 28 <artifactId>spring-context</artifactId> 29 <version>4.3.1.RELEASE</version> 30 </dependency> 31 32 <!--Spring Web + Spring MVC--> 33 <dependency> 34 <groupId>org.springframework</groupId> 35 <artifactId>spring-web</artifactId> 36 <version>4.3.1.RELEASE</version> 37 </dependency> 38 <dependency> 39 <groupId>org.springframework</groupId> 40 <artifactId>spring-webmvc</artifactId> 41 <version>4.3.1.RELEASE</version> 42 </dependency> 43 44 <!--NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config--> 45 <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> 46 <dependency> 47 <groupId>javax.servlet</groupId> 48 <artifactId>jstl</artifactId> 49 <version>1.2</version> 50 </dependency> 51 52 <dependency> 53 <groupId>taglibs</groupId> 54 <artifactId>standard</artifactId> 55 <version>1.1.2</version> 56 </dependency> 57 58 <!--servlet/jsp api start--> 59 <dependency> 60 <groupId>javax.servlet</groupId> 61 <artifactId>servlet-api</artifactId> 62 <version>2.5</version> 63 </dependency> 64 65 <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api --> 66 <dependency> 67 <groupId>javax.servlet.jsp</groupId> 68 <artifactId>jsp-api</artifactId> 69 <version>2.2</version> 70 </dependency> 71 72 <!--mysql driver--> 73 <dependency> 74 <groupId>mysql</groupId> 75 <artifactId>mysql-connector-java</artifactId> 76 <version>5.1.6</version> 77 </dependency> 78 79 <!--jdbc--> 80 <dependency> 81 <groupId>org.springframework</groupId> 82 <artifactId>spring-jdbc</artifactId> 83 <version>3.0.5.RELEASE</version> 84 </dependency> 85 86 <!--c3p0--> 87 <dependency> 88 <groupId>com.mchange</groupId> 89 <artifactId>c3p0</artifactId> 90 <version>0.9.5.1</version> 91 </dependency> 92 93 <!--aspectj--> 94 <dependency> 95 <groupId>org.aspectj</groupId> 96 <artifactId>aspectjweaver</artifactId> 97 <version>1.8.6</version> 98 </dependency> 99 100 <dependency> 101 <groupId>org.aspectj</groupId> 102 <artifactId>aspectjrt</artifactId> 103 <version>1.8.6</version> 104 </dependency> 105 106 <!--mybatis--> 107 <dependency> 108 <groupId>org.mybatis</groupId> 109 <artifactId>mybatis</artifactId> 110 <version>3.3.0</version> 111 </dependency> 112 113 <!--mybatis spring整合--> 114 <dependency> 115 <groupId>org.mybatis</groupId> 116 <artifactId>mybatis-spring</artifactId> 117 <version>1.2.3</version> 118 </dependency> 119 120 </dependencies> 121 122 <build> 123 <finalName>SpringJDBCTest1</finalName> 124 125 <plugins> 126 <!--servlet容器 jetty插件--> 127 <plugin> 128 <groupId>org.eclipse.jetty</groupId> 129 <artifactId>jetty-maven-plugin</artifactId> 130 <version>9.3.10.v20160621</version> 131 </plugin> 132 133 <!--mybatis 逆向工程插件--> 134 <plugin> 135 <groupId>org.mybatis.generator</groupId> 136 <artifactId>mybatis-generator-maven-plugin</artifactId> 137 <version>1.3.2</version> 138 <configuration> 139 <verbose>true</verbose> 140 <overwrite>true</overwrite> 141 </configuration> 142 </plugin> 143 <plugin> 144 <groupId>org.apache.maven.plugins</groupId> 145 <artifactId>maven-compiler-plugin</artifactId> 146 <configuration> 147 <source>1.7</source> 148 <target>1.7</target> 149 </configuration> 150 </plugin> 151 </plugins> 152 </build> 153 </project>
2 src-main下建java源包(设置成source folder),建com.springjdbc根包,
分别建controller,dao,entities等包:
EmployeeController.java
1 package com.springjdbc.controller; 2 3 import com.springjdbc.dao.EmployeeDao; 4 import com.springjdbc.entities.Employee; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 import org.springframework.jdbc.core.BeanPropertyRowMapper; 8 import org.springframework.jdbc.core.JdbcTemplate; 9 import org.springframework.jdbc.core.RowMapper; 10 import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; 11 import org.springframework.stereotype.Controller; 12 import org.springframework.web.bind.annotation.RequestMapping; 13 import org.springframework.web.bind.annotation.RequestMethod; 14 15 import java.util.List; 16 import java.util.Map; 17 18 /** 19 * Created by wanggenshen_sx on 2016/12/23. 20 */ 21 @Controller 22 public class EmployeeController { 23 24 private ApplicationContext ctx=null; 25 private JdbcTemplate jdbcTemplate=null; 26 private EmployeeDao employeeDao ; 27 private NamedParameterJdbcTemplate namedParameterJdbcTemplate=null; 28 { 29 ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); 30 jdbcTemplate =(JdbcTemplate) ctx.getBean("jdbcTemplate"); 31 employeeDao = ctx.getBean(EmployeeDao.class); 32 namedParameterJdbcTemplate=(NamedParameterJdbcTemplate) ctx.getBean("namedParameterJdbcTemplate"); 33 34 } 35 36 @RequestMapping(value = "/listAllEmployee",method = RequestMethod.GET) 37 public String list(Map<String,Object> map){ 38 String sql="SELECT id, lastName,email FROM employee"; 39 RowMapper<Employee> rowMapper=new BeanPropertyRowMapper<>(Employee.class); 40 List<Employee> employees=jdbcTemplate.query(sql, rowMapper); 41 map.put("employees",employees); 42 return "list"; 43 } 44 }
EmployeeDao.java
package com.springjdbc.dao; import com.springjdbc.entities.Employee; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; /** * Created by wanggenshen_sx on 2016/12/23. */ @Repository public class EmployeeDao { @Autowired private JdbcTemplate jdbcTemplate; public Employee get(int id){ String sql="SELECT id, lastName,email FROM employee WHERE id=?"; RowMapper<Employee> rowMapper=new BeanPropertyRowMapper<>(Employee.class); Employee employee=jdbcTemplate.queryForObject(sql, rowMapper,id); return employee; } }
Employee.java
1 package com.springjdbc.entities; 2 3 /** 4 * Created by wanggenshen_sx on 2016/12/23. 5 */ 6 public class Employee { 7 8 private int id; 9 private String lastName; 10 private String email; 11 12 public int getId() { 13 return id; 14 } 15 public void setId(int id) { 16 this.id = id; 17 } 18 public String getLastName() { 19 return lastName; 20 } 21 public void setLastName(String lastName) { 22 this.lastName = lastName; 23 } 24 public String getEmail() { 25 return email; 26 } 27 public void setEmail(String email) { 28 this.email = email; 29 } 30 @Override 31 public String toString() { 32 return "Employee [id=" + id + ", lastName=" + lastName + ", email=" 33 + email+" ]" ; 34 } 35 }
3 webapp --- WEB-INF --下建views包,新建显示页面
list.jsp:
1 <%-- 2 Created by IntelliJ IDEA. 3 User: wanggenshen_sx 4 Date: 2016/12/23 5 Time: 16:56 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 10 <html> 11 <head> 12 <title>Show Page</title> 13 </head> 14 <body> 15 <table border="1" cellspacing="0" cellpadding="10"> 16 <tr> 17 <th>ID</th> 18 <th>LastName</th> 19 <th>Email</th> 20 </tr> 21 22 <c:forEach items="${employees}" var="emp"> 23 <tr> 24 <th>${emp.id}</th> 25 <th>${emp.lastName}</th> 26 <th>${emp.email}</th> 27 </tr> 28 </c:forEach> 29 </table> 30 </body> 31 </html>
4 配置
1)web.xml
1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 <web-app xmlns="http://java.sun.com/xml/ns/javaee" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 7 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 8 version="3.0"> 9 10 <!--配置springmvc DispatcherServlet--> 11 <servlet> 12 <servlet-name>springmvc-servlet</servlet-name> 13 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 14 <init-param> 15 <param-name>contextConfigLocation</param-name> 16 <param-value>classpath:springmvc-servlet.xml</param-value> 17 </init-param> 18 19 <load-on-startup>1</load-on-startup> 20 </servlet> 21 22 <servlet-mapping> 23 <servlet-name>springmvc-servlet</servlet-name> 24 <url-pattern>/</url-pattern> 25 </servlet-mapping> 26 27 <context-param> 28 <param-name>contextConfigLocation</param-name> 29 <param-value>classpath:applicationContext.xml</param-value> 30 </context-param> 31 32 <listener> 33 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 34 </listener> 35 36 </web-app>
note:此处加红处显示,最好是指定classpath,这样一眼看到就知道文件的位置。
2)resources包:新建spring配置文件(注意psring配置文件中扫描的包一定是controller包以外的包)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 8 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 9 10 11 <context:component-scan base-package="com.springjdbc.dao"></context:component-scan> 12 13 <!-- 导入资源文件 --> 14 <context:property-placeholder location="classpath:db.properties"/> 15 <!-- 配置C3P0数据源 --> 16 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 17 <property name="user" value="${jdbc.user}"></property> 18 <property name="password" value="${jdbc.password}"></property> 19 <property name="driverClass" value="${jdbc.driverClass}"></property> 20 <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> 21 22 <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property> 23 <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> 24 25 </bean> 26 27 <!-- 配置spring-jdbcTemplate模板 --> 28 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 29 <property name="dataSource" ref="dataSource"></property> 30 </bean> 31 32 <!-- 配置 NamedParameterJdbcTemplate, 33 该对象可以使用具名参数, 其没有无参数的构造器, 所以必须为其构造器指定参数 --> 34 <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> 35 <constructor-arg ref="dataSource"></constructor-arg> 36 </bean> 37 38 <!-- 配置事务管理器 --> 39 <bean id="transactionManager" 40 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 41 <property name="dataSource" ref="dataSource"></property> 42 </bean> 43 44 <!-- 启用事务注解 --> 45 <tx:annotation-driven transaction-manager="transactionManager"/> 46 47 <!-- spring注解模式配置 --> 48 <context:annotation-config/> 49 50 </beans>
3)SpringMVC的配置文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 8 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 9 10 11 <context:component-scan base-package="com.springjdbc.dao"></context:component-scan> 12 13 <!-- 导入资源文件 --> 14 <context:property-placeholder location="classpath:db.properties"/> 15 <!-- 配置C3P0数据源 --> 16 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 17 <property name="user" value="${jdbc.user}"></property> 18 <property name="password" value="${jdbc.password}"></property> 19 <property name="driverClass" value="${jdbc.driverClass}"></property> 20 <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> 21 22 <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property> 23 <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> 24 25 </bean> 26 27 <!-- 配置spring-jdbcTemplate模板 --> 28 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 29 <property name="dataSource" ref="dataSource"></property> 30 </bean> 31 32 <!-- 配置 NamedParameterJdbcTemplate, 33 该对象可以使用具名参数, 其没有无参数的构造器, 所以必须为其构造器指定参数 --> 34 <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> 35 <constructor-arg ref="dataSource"></constructor-arg> 36 </bean> 37 38 <!-- 配置事务管理器 --> 39 <bean id="transactionManager" 40 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 41 <property name="dataSource" ref="dataSource"></property> 42 </bean> 43 44 <!-- 启用事务注解 --> 45 <tx:annotation-driven transaction-manager="transactionManager"/> 46 47 <!-- spring注解模式配置 --> 48 <context:annotation-config/> 49 50 </beans>
4)连接数据库的配置文件:db.properties
jdbc.user=root jdbc.password=920614 jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql:///mytest?useUnicode=true&characterEncoding=utf-8 jdbc.initPoolSize=5 jdbc.maxPoolSize=10
项目架构: