spring-springMVC-MyBatis整合
Spring-SpringMVC-MyBatis整合
创建实体类:
1 package com.neuedu.ssm.entity; 2 3 public class Employee { 4 5 private Integer id; 6 private String lastName; 7 private boolean gender; 8 private String email; 9 public Employee() { 10 super(); 11 // TODO Auto-generated constructor stub 12 } 13 public Employee(Integer id, String lastName, boolean gender, String email) { 14 super(); 15 this.id = id; 16 this.lastName = lastName; 17 this.gender = gender; 18 this.email = email; 19 } 20 public Integer getId() { 21 return id; 22 } 23 public void setId(Integer id) { 24 this.id = id; 25 } 26 public String getLastName() { 27 return lastName; 28 } 29 public void setLastName(String lastName) { 30 this.lastName = lastName; 31 } 32 public boolean isGender() { 33 return gender; 34 } 35 public void setGender(boolean gender) { 36 this.gender = gender; 37 } 38 public String getEmail() { 39 return email; 40 } 41 public void setEmail(String email) { 42 this.email = email; 43 } 44 @Override 45 public String toString() { 46 return "Employee [id=" + id + ", lastName=" + lastName + ", gender=" 47 + gender + ", email=" + email + "]"; 48 } 49 50 51 52 }
创建service层:
1 package com.neuedu.ssm.service; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 8 import com.neuedu.ssm.entity.Employee; 9 import com.neuedu.ssm.mapper.EmployeeMapper; 10 11 @Service 12 public class EmployeeService { 13 14 @Autowired 15 private EmployeeMapper mapper; 16 17 public List<Employee> getList(){ 18 19 return mapper.getList(); 20 21 } 22 }
创建mapper接口:
1 package com.neuedu.ssm.mapper; 2 3 import java.util.List; 4 5 import com.neuedu.ssm.entity.Employee; 6 7 public interface EmployeeMapper { 8 9 public List<Employee> getList(); 10 11 12 }
创建controller层:
1 package com.neuedu.ssm.controller; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 10 import com.neuedu.ssm.entity.Employee; 11 import com.neuedu.ssm.service.EmployeeService; 12 13 @Controller 14 public class EmployeeController { 15 16 @Autowired 17 private EmployeeService service; 18 19 @RequestMapping("/getList") 20 public String getList(Map<String, Object> map){ 21 List<Employee> list = service.getList(); 22 map.put("list", list); 23 for (Employee employee : list) { 24 System.out.println(employee); 25 } 26 return "success"; 27 } 28 }
创建jdbc.properties文件:
1 jdbc.jdbcUrl=jdbc:mysql://localhost:3306/mybatis 2 jdbc.driverClass=com.mysql.jdbc.Driver 3 jdbc.user=root 4 jdbc.password=root
创建applicationContext.xml:
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:p="http://www.springframework.org/schema/p" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 10 11 <!-- 配置扫描包 --> 12 <context:component-scan base-package="com.neuedu.ssm"> 13 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 14 <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> 15 </context:component-scan> 16 17 <!-- 加载外部文件 --> 18 <context:property-placeholder location="classpath:jdbc.properties"/> 19 20 <!-- 配置数据库连接池 c3p0--> <!-- ComboPooledDataSource存储数据源接口池 --> 21 <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 22 <property name="user" value="${jdbc.user}"></property> 23 <property name="password" value="${jdbc.password}"></property> 24 <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> 25 <property name="driverClass" value="${jdbc.driverClass}"></property> 26 </bean> 27 28 29 <!-- 配置事务管理器 --> 30 <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 31 <constructor-arg name="dataSource" ref="comboPooledDataSource"/> 32 </bean> 33 34 <!-- 开启基于注解的事务 --> 35 <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/> 36 37 38 <!-- 配置 SqlSessionFactory --> 39 <bean class="org.mybatis.spring.SqlSessionFactoryBean"> 40 <property name="dataSource" ref="comboPooledDataSource"></property> 41 <!-- 配置 mybatis 配置文件的位置和名称 --> 42 <property name="configLocation" value="classpath:mybatis-config.xml"></property> 43 </bean> 44 45 <!-- 配置mapper接口扫描包 --> 46 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 47 <property name="basePackage" value="com.neuedu.ssm.mapper"></property> 48 </bean> 49 50 </beans>
创建springMVC.xml:
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:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 9 <!-- 配置扫描包,以完成Bean创建和自动依赖注入的功能 --> 10 <context:component-scan base-package="com.neuedu.ssm" use-default-filters="false" > 11 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 12 <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> 13 </context:component-scan> 14 15 <!-- 配置springMVC视图解析器,将逻辑视图转换成物理视图 --> 16 <!--(转发) 对转向页面的路径解析。prefix(前缀)+returnVal(方法的返回值)+ suffix(后缀) --> 17 <!-- /WEB-INF/views/+success+.jsp --> 18 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 19 <property name="prefix" value="/WEB-INF/views/"></property> 20 <property name="suffix" value=".jsp"></property> 21 </bean> 22 23 24 <!-- 如果当前路径是/.. 则交给相应的视图解析器直接解析为视图,不用经过handler方法直接到达目标页面 --> 25 <!-- path="/.."随便指定路径,访问该路径时都可以得到视图: /WEB-INF/views/success.jsp --> 26 <mvc:view-controller path="/" view-name="success"/> 27 28 <!--相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean, 29 配置一些messageconverter。即解决了@Controller注解的使用前提配置 --> 30 <mvc:annotation-driven/> 31 32 <!-- 对于静态资源文件如【js/css/图片】的访问,需要在spingmvc配置文件中配置一个标签 --> 33 <!--springMVC DispatcherServlet拦截了所有请求,因此需要配置mvc:default-servlet-handler,可以映射静态资源的访问请求 --> 34 <mvc:default-servlet-handler/> 35 </beans>
创建mybatis-config.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 <!-- settings标签用于对mybatis的属性设置 8 mapUnderscoreToCamelCase:可以将数据库中下划线分隔的字段转化为 9 JavaBean对象驼峰式命名的属性 10 --> 11 <settings> 12 <!-- 下划线转驼峰式命名 --> 13 <setting name="mapUnderscoreToCamelCase" value="true"/> 14 15 <!-- 开启懒加载机制 ,默认值为true--> 16 <setting name="lazyLoadingEnabled" value="true"/> 17 <!-- 开启的话,每个属性都会直接全部加载出来;禁用的话,只会按需加载出来 --> 18 <setting name="aggressiveLazyLoading" value="false"/> 19 </settings> 20 21 22 </configuration>
web.xml配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 3 <display-name>ssm-integration</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <!-- spring IOC容器 --> 14 <context-param> 15 <param-name>contextConfigLocation</param-name> 16 <param-value>classpath:applicationContext.xml</param-value> 17 </context-param> 18 19 <!--spring监听器 框架自带,创建IOC容器 --> 20 <listener> 21 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 22 </listener> 23 24 25 26 <!-- springMVC前端控制器 创建springMVC的IOC容器--> 27 <servlet> 28 <servlet-name>DispatcherServlet</servlet-name> 29 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 30 <init-param> 31 <param-name>contextConfigLocation</param-name> 32 <param-value>classpath:springMVC.xml</param-value> 33 </init-param> 34 <load-on-startup>1</load-on-startup> 35 </servlet> 36 37 <!-- Map all requests to the DispatcherServlet for handling --> 38 <servlet-mapping> 39 <servlet-name>DispatcherServlet</servlet-name> 40 <url-pattern>/</url-pattern> 41 </servlet-mapping> 42 43 44 <!-- 针对POST请求乱码的处理:CharacterEncodingFilter --> 45 <filter> 46 <filter-name>CharacterEncodingFilter</filter-name> 47 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 48 <init-param> 49 <param-name>encoding</param-name> 50 <param-value>utf-8</param-value> 51 </init-param> 52 </filter> 53 <filter-mapping> 54 <filter-name>CharacterEncodingFilter</filter-name> 55 <url-pattern>/*</url-pattern> 56 </filter-mapping> 57 58 59 <!--HiddenHttpMethodFilter过滤器:浏览器form表单只支持GET与POST请求,而DELETE、PUT等method并不支持, 60 spring3.0添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求 --> 61 <filter> 62 <filter-name>HiddenHttpMethodFilter</filter-name> 63 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 64 </filter> 65 <filter-mapping> 66 <filter-name>HiddenHttpMethodFilter</filter-name> 67 <url-pattern>/*</url-pattern> 68 </filter-mapping> 69 70 </web-app>
pom.xml配置:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>com.neuedu.ssm</groupId> 4 <artifactId>ssm-integration</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>war</packaging> 7 8 <dependencies> 9 <!-- SPRING IOC --> 10 <dependency> 11 <groupId>org.springframework</groupId> 12 <artifactId>spring-beans</artifactId> 13 <version>4.0.0.RELEASE</version> 14 </dependency> 15 16 <dependency> 17 <groupId>org.springframework</groupId> 18 <artifactId>spring-core</artifactId> 19 <version>4.0.0.RELEASE</version> 20 </dependency> 21 22 <dependency> 23 <groupId>org.springframework</groupId> 24 <artifactId>spring-context</artifactId> 25 <version>4.0.0.RELEASE</version> 26 </dependency> 27 28 <dependency> 29 <groupId>org.springframework</groupId> 30 <artifactId>spring-expression</artifactId> 31 <version>4.0.0.RELEASE</version> 32 </dependency> 33 34 <dependency> 35 <groupId>org.springframework</groupId> 36 <artifactId>spring-aspects</artifactId> 37 <version>4.0.0.RELEASE</version> 38 </dependency> 39 <!-- mysql --> 40 <dependency> 41 <groupId>mysql</groupId> 42 <artifactId>mysql-connector-java</artifactId> 43 <version>5.1.37</version> 44 </dependency> 45 <!-- C3P0 --> 46 <dependency> 47 <groupId>com.mchange</groupId> 48 <artifactId>c3p0</artifactId> 49 <version>0.9.5.2</version> 50 </dependency> 51 52 <dependency> 53 <groupId>junit</groupId> 54 <artifactId>junit</artifactId> 55 <version>4.9</version> 56 </dependency> 57 <!-- aop AND Transaction --> 58 <dependency> 59 <groupId>net.sourceforge.cglib</groupId> 60 <artifactId>com.springsource.net.sf.cglib</artifactId> 61 <version>2.2.0</version> 62 </dependency> 63 64 <dependency> 65 <groupId>org.aspectj</groupId> 66 <artifactId>aspectjweaver</artifactId> 67 <version>1.6.8</version> 68 </dependency> 69 70 71 <!-- JDBCTemplate and Transaction --> 72 <dependency> 73 <groupId>org.springframework</groupId> 74 <artifactId>spring-jdbc</artifactId> 75 <version>4.0.0.RELEASE</version> 76 </dependency> 77 <dependency> 78 <groupId>org.springframework</groupId> 79 <artifactId>spring-orm</artifactId> 80 <version>4.0.0.RELEASE</version> 81 </dependency> 82 <dependency> 83 <groupId>org.springframework</groupId> 84 <artifactId>spring-web</artifactId> 85 <version>4.0.0.RELEASE</version> 86 </dependency> 87 <!-- springmvc --> 88 <dependency> 89 <groupId>org.springframework</groupId> 90 <artifactId>spring-webmvc</artifactId> 91 <version>4.0.0.RELEASE</version> 92 </dependency> 93 <!-- upload --> 94 <dependency> 95 <groupId>commons-fileupload</groupId> 96 <artifactId>commons-fileupload</artifactId> 97 <version>1.3.1</version> 98 </dependency> 99 <!-- JSTL --> 100 <dependency> 101 <groupId>org.apache.taglibs</groupId> 102 <artifactId>taglibs-standard-impl</artifactId> 103 <version>1.2.1</version> 104 </dependency> 105 <dependency> 106 <groupId>org.apache.taglibs</groupId> 107 <artifactId>taglibs-standard-spec</artifactId> 108 <version>1.2.1</version> 109 </dependency> 110 111 112 <!--JSON --> 113 <dependency> 114 <groupId>com.fasterxml.jackson.core</groupId> 115 <artifactId>jackson-annotations</artifactId> 116 <version>2.1.5</version> 117 </dependency> 118 119 <dependency> 120 <groupId>com.fasterxml.jackson.core</groupId> 121 <artifactId>jackson-databind</artifactId> 122 <version>2.1.5</version> 123 </dependency> 124 125 <dependency> 126 <groupId>com.fasterxml.jackson.core</groupId> 127 <artifactId>jackson-core</artifactId> 128 <version>2.1.5</version> 129 </dependency> 130 131 132 <!-- jsp-servlet --> 133 <dependency> 134 <groupId>javax.servlet</groupId> 135 <artifactId>servlet-api</artifactId> 136 <version>2.5</version> 137 <scope>provided</scope> 138 </dependency> 139 <dependency> 140 <groupId>javax.servlet</groupId> 141 <artifactId>jsp-api</artifactId> 142 <version>2.0</version> 143 <scope>provided</scope> 144 </dependency> 145 146 147 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> 148 <dependency> 149 <groupId>org.mybatis</groupId> 150 <artifactId>mybatis</artifactId> 151 <version>3.4.1</version> 152 </dependency> 153 154 <!-- mybatis-spring整合 --> 155 <dependency> 156 <groupId>org.mybatis</groupId> 157 <artifactId>mybatis-spring</artifactId> 158 <version>1.3.0</version> 159 </dependency> 160 161 162 <!--mysql驱动 --> 163 <dependency> 164 <groupId>mysql</groupId> 165 <artifactId>mysql-connector-java</artifactId> 166 <version>5.1.37</version> 167 </dependency> 168 <!-- oracle驱动 --> 169 <dependency> 170 <groupId>ojdbc</groupId> 171 <artifactId>ojdbc</artifactId> 172 <version>6</version> 173 </dependency> 174 <!-- https://mvnrepository.com/artifact/log4j/log4j --> 175 <dependency> 176 <groupId>log4j</groupId> 177 <artifactId>log4j</artifactId> 178 <version>1.2.17</version> 179 </dependency> 180 181 </dependencies> 182 183 <!--tomcat7插件 --> 184 <build> 185 <plugins> 186 <plugin> 187 <groupId>org.apache.tomcat.maven</groupId> 188 <artifactId>tomcat7-maven-plugin</artifactId> 189 <version>2.2</version> 190 <configuration> 191 <path>/ssm-integration</path> 192 <port>8080</port> 193 </configuration> 194 </plugin> 195 </plugins> 196 </build> 197 198 199 </project>
运行结果:
posted on 2017-12-25 20:41 ALWAYS☆REMIND 阅读(134) 评论(0) 编辑 收藏 举报