spring+mybatis 整合
项目目录:
一、导入pom.xml依赖 最下边有
二、在applicationContext.xml配置连接器和数据库的数据源
三、流程:用户请求数据,springmvc解析url,通过控制器和适配器 找到对应的Controller (UserController),通过自动注入的userService找到里面的hello方法,在查询出结果return值回到userController,
在通过model.addAttribute向页面传参,最后return到页面中。
四、这是测试类的加载配置文件和自动注入(UserDAOTest)来测试更加方便
配置事务:
web.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 5 version="3.0"> 6 <!-- 加载applicationContext.xml配置文件 --> 7 <context-param> 8 <param-name>contextConfigLocation</param-name> 9 <param-value>classpath:applicationContext.xml</param-value> 10 </context-param> 11 <!-- 设置编码为UTF-8 --> 12 <filter> 13 <description>字符集过滤器</description> 14 <filter-name>encodingFilter</filter-name> 15 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 16 <init-param> 17 <description>字符集编码</description> 18 <param-name>encoding</param-name> 19 <param-value>UTF-8</param-value> 20 </init-param> 21 </filter> 22 <filter-mapping> 23 <filter-name>encodingFilter</filter-name> 24 <url-pattern>/*</url-pattern> 25 </filter-mapping> 26 27 <!-- spring监听器 --> 28 <listener> 29 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 30 </listener> 31 32 <listener> 33 <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 34 </listener> 35 36 <!-- 配置Servlet --> 37 <servlet> 38 <servlet-name>springmvc1</servlet-name> 39 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 40 <init-param> 41 <param-name>contextConfigLocation</param-name> 42 <param-value>classpath:springmvc.xml</param-value> 43 </init-param> 44 <load-on-startup>1</load-on-startup> 45 </servlet> 46 47 <servlet-mapping> 48 <servlet-name>springmvc1</servlet-name> 49 <url-pattern>*.do</url-pattern> 50 </servlet-mapping> 51 52 53 <!-- 程序加载时的页面 --> 54 <welcome-file-list> 55 <welcome-file>/index.jsp</welcome-file> 56 </welcome-file-list> 57 58 </web-app>
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-3.2.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 9 10 <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --> 11 <context:component-scan base-package="com.xf"/> 12 <mvc:annotation-driven/> 13 14 <!-- 逻辑视图与物理视图的映射 --> 15 <bean 16 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 17 <property name="prefix" value="/WEB-INF/user/"></property> 18 <property name="suffix" value=".jsp"></property> 19 </bean> 20 21 </beans>
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:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 7 8 <!--<context:annotation-config></context:annotation-config>--> 9 10 <!-- 注解注入--> 11 <context:component-scan base-package="com"> 12 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter> 13 </context:component-scan> 14 15 <!--导入jdbc.properties文件 --> 16 <context:property-placeholder location="classpath:mysql.properties" 17 ignore-unresolvable="true"/> 18 19 <!--配置mysql数据源 --> 20 <bean id="dataSource" 21 class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 22 <property name="driverClassName"> 23 <value>${mysql.driverClassName}</value> 24 </property> 25 <property name="url"> 26 <value>${mysql.url} 27 </value> 28 </property> 29 <property name="username"> 30 <value>${mysql.username}</value> 31 </property> 32 <property name="password"> 33 <value>${mysql.password}</value> 34 </property> 35 </bean> 36 37 <!-- myBatis文件 --> 38 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 39 <property name="dataSource" ref="dataSource"/> 40 <!-- 加载mybatis-config配置文件,里面设置别名 --> 41 <!-- <property name="configLocation" value="/WEB-INF/mybatis-config.xml"/>--> 42 <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置,并且可以扫描实体中的别名注解 --> 43 <property name="typeAliasesPackage" value="com.etc.entity"/> 44 <!-- 自动扫描xml目录, 扫描DAO.xml--> 45 <property name="mapperLocations" value="classpath*:*DAO.xml"/> 46 </bean> 47 48 49 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 50 <!-- 自动扫描dao目录, 扫描interface --> 51 <property name="basePackage" value="com.etc.dao"/> 52 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 53 <!--<property name="annotationClass" value="com.xf.dao.MyBatisRepository"/>--> 54 </bean> 55 56 57 <!--事务 --> 58 <aop:config> 59 <aop:pointcut id="transactionPointcut" expression="execution(* com.etc.servise.*.*(..))"/> 60 <aop:advisor pointcut-ref="transactionPointcut" 61 advice-ref="transactionAdvice"/> 62 </aop:config> 63 64 <!-- 注解方式配置事物 --> 65 <!-- <tx:annotation-driven transaction-manager="transactionManager" /> --> 66 <!-- 拦截器方式配置事物 --> 67 68 <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> 69 <tx:attributes> 70 <tx:method name="*" propagation="REQUIRED"/> 71 </tx:attributes> 72 </tx:advice> 73 74 <!-- 配置事务管理器 --> 75 <bean id="transactionManager" 76 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 77 <property name="dataSource" ref="dataSource"/> 78 </bean> 79 </beans>
com/etc/dao/UserDAO.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.etc.dao.UserDAO"> 4 <select id="queryById" resultType="user"> 5 select * from t_user where id=#{id} 6 </select> 7 8 <insert id="save"> 9 insert into t_user(name) values(#{name}) 10 </insert> 11 </mapper>
com.etc.servise.UserService:
1 package com.etc.servise; 2 3 import com.etc.dao.UserDAO; 4 import com.etc.entity.User; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 8 @Service 9 public class UserService { 10 11 @Autowired 12 private UserDAO userDAO; 13 14 public User hello(int id){ 15 System.out.println("Hello spring!"); 16 User user = userDAO.queryById(id); 17 18 User user1=new User(); 19 user1.setName("123"); 20 userDAO.save(user1); 21 22 23 User user2=new User(); 24 user2.setName("123"); 25 userDAO.save(user2); 26 27 return user; 28 } 29 }
com.etc.controller.UserController:
1 package com.etc.controller; 2 3 import com.etc.entity.User; 4 import com.etc.servise.UserService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 10 @Controller 11 public class UserController { 12 13 @Autowired 14 private UserService userService; 15 16 @RequestMapping("/hello") 17 public String hello(int id, Model model){ 18 System.out.println("Hello Springmvc"); 19 User user = userService.hello(id); 20 System.out.println(user); 21 model.addAttribute("user", user); 22 return "hello"; 23 } 24 }
com.etc.dao.UserDAOTest:
1 package com.etc.dao; 2 3 import com.etc.entity.User; 4 import org.junit.Test; 5 import org.junit.runner.RunWith; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.test.context.ContextConfiguration; 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 10 @RunWith(SpringJUnit4ClassRunner.class) 11 @ContextConfiguration(locations = "classpath:applicationContext.xml") 12 public class UserDAOTest { 13 14 @Autowired 15 private UserDAO userDAO; 16 17 @Test 18 public void testQueryById(){ 19 //测试驱动开发 20 User user = userDAO.queryById(5); 21 System.out.println(user); 22 } 23 24 @Test 25 public void save(){ 26 User user1=new User(); 27 user1.setName("123"); 28 userDAO.save(user1); 29 } 30 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.etc.entity; 2 3 import java.util.Date; 4 5 public class User { 6 private int id; 7 private String name; 8 private double sal; 9 private Date birth; 10 11 12 public User() { 13 } 14 15 public User(int id, String name, double sal, Date birth) { 16 this.id = id; 17 this.name = name; 18 this.sal = sal; 19 this.birth = birth; 20 } 21 22 public int getId() { 23 return id; 24 } 25 26 public void setId(int id) { 27 this.id = id; 28 } 29 30 public String getName() { 31 return name; 32 } 33 34 public void setName(String name) { 35 this.name = name; 36 } 37 38 public double getSal() { 39 return sal; 40 } 41 42 public void setSal(double sal) { 43 this.sal = sal; 44 } 45 46 public Date getBirth() { 47 return birth; 48 } 49 50 public void setBirth(Date birth) { 51 this.birth = birth; 52 } 53 54 55 56 @Override 57 public String toString() { 58 return "User{" + 59 "id=" + id + 60 ", name='" + name + '\'' + 61 ", sal=" + sal + 62 ", birth=" + birth + 63 '}'; 64 } 65 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.etc</groupId> 8 <artifactId>ssm01</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>war</packaging> 11 12 <name>ssm01 Maven Webapp</name> 13 <!-- FIXME change it to the project's website --> 14 <url>http://www.example.com</url> 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <maven.compiler.source>1.7</maven.compiler.source> 19 <maven.compiler.target>1.7</maven.compiler.target> 20 <spring.version>4.3.24.RELEASE</spring.version> 21 <mybatis.version>3.5.1</mybatis.version> 22 <mybatis-spring.version>1.3.2</mybatis-spring.version> 23 </properties> 24 25 <dependencies> 26 <!--白盒测试工具 --> 27 <dependency> 28 <groupId>junit</groupId> 29 <artifactId>junit</artifactId> 30 <version>4.12</version> 31 </dependency> 32 <!--控制日志信息输送的目的地是控制台、文件、GUI组件、甚至是套接口服务器 --> 33 <dependency> 34 <groupId>log4j</groupId> 35 <artifactId>log4j</artifactId> 36 <version>1.2.17</version> 37 </dependency> 38 <dependency> 39 <groupId>org.slf4j</groupId> 40 <artifactId>slf4j-api</artifactId> 41 <version>1.7.5</version> 42 </dependency> 43 <!--解析json --> 44 <dependency> 45 <groupId>org.codehaus.jackson</groupId> 46 <artifactId>jackson-mapper-asl</artifactId> 47 <version>1.9.12</version> 48 </dependency> 49 <!--处理编码 --> 50 <dependency> 51 <groupId>commons-codec</groupId> 52 <artifactId>commons-codec</artifactId> 53 <version>1.7</version> 54 </dependency> 55 <!-- mybatis是ibatis 的升级版。是一种 orm 工具,简化数据库操作 --> 56 <dependency> 57 <groupId>org.mybatis</groupId> 58 <artifactId>mybatis</artifactId> 59 <version>${mybatis.version}</version> 60 </dependency> 61 <!-- 是一个用来整合 MyBatis 和 Spring 框架的小类库 --> 62 <dependency> 63 <groupId>org.mybatis</groupId> 64 <artifactId>mybatis-spring</artifactId> 65 <version>${mybatis-spring.version}</version> 66 </dependency> 67 68 <!--用来帮助进行IO功能开发 --> 69 <dependency> 70 <groupId>commons-io</groupId> 71 <artifactId>commons-io</artifactId> 72 <version>2.0.1</version> 73 </dependency> 74 <!-- Spring --> 75 <dependency> 76 <groupId>org.springframework</groupId> 77 <artifactId>spring-core</artifactId> 78 <version>${spring.version}</version> 79 </dependency> 80 <dependency> 81 <groupId>org.springframework</groupId> 82 <artifactId>spring-context</artifactId> 83 <version>${spring.version}</version> 84 </dependency> 85 <dependency> 86 <groupId>org.springframework</groupId> 87 <artifactId>spring-web</artifactId> 88 <version>${spring.version}</version> 89 </dependency> 90 <dependency> 91 <groupId>org.springframework</groupId> 92 <artifactId>spring-context-support</artifactId> 93 <version>${spring.version}</version> 94 </dependency> 95 <dependency> 96 <groupId>org.springframework</groupId> 97 <artifactId>spring-webmvc</artifactId> 98 <version>${spring.version}</version> 99 </dependency> 100 <dependency> 101 <groupId>org.springframework</groupId> 102 <artifactId>spring-test</artifactId> 103 <version>${spring.version}</version> 104 </dependency> 105 <!-- aop --> 106 <dependency> 107 <groupId>org.springframework</groupId> 108 <artifactId>spring-aop</artifactId> 109 <version>${spring.version}</version> 110 </dependency> 111 <dependency> 112 <groupId>org.aspectj</groupId> 113 <artifactId>aspectjweaver</artifactId> 114 <version>1.8.13</version> 115 </dependency> 116 <dependency> 117 <groupId>cglib</groupId> 118 <artifactId>cglib</artifactId> 119 <version>3.2.4</version> 120 </dependency> 121 <!--tx--> 122 <dependency> 123 <groupId>org.springframework</groupId> 124 <artifactId>spring-orm</artifactId> 125 <version>${spring.version}</version> 126 </dependency> 127 <dependency> 128 <groupId>org.springframework</groupId> 129 <artifactId>spring-tx</artifactId> 130 <version>${spring.version}</version> 131 </dependency> 132 <!-- jackson --> 133 <dependency> 134 <groupId>com.fasterxml.jackson.core</groupId> 135 <artifactId>jackson-core</artifactId> 136 <version>2.9.3</version> 137 </dependency> 138 <dependency> 139 <groupId>com.fasterxml.jackson.core</groupId> 140 <artifactId>jackson-annotations</artifactId> 141 <version>2.9.3</version> 142 </dependency> 143 <dependency> 144 <groupId>com.fasterxml.jackson.core</groupId> 145 <artifactId>jackson-databind</artifactId> 146 <version>2.9.3</version> 147 </dependency> 148 <!-- jdbc driver --> 149 <dependency> 150 <groupId>mysql</groupId> 151 <artifactId>mysql-connector-java</artifactId> 152 <version>5.1.22</version> 153 </dependency> 154 <!-- JSTL(JSP Standard Tag Library ,JSP标准标签库)是一个不断完善的开放源代码的JSP标签 --> 155 <dependency> 156 <groupId>javax.servlet</groupId> 157 <artifactId>jstl</artifactId> 158 <version>1.2</version> 159 </dependency> 160 <!-- Servlet包含的类以及方法 --> 161 <dependency> 162 <groupId>javax.servlet</groupId> 163 <artifactId>servlet-api</artifactId> 164 <version>3.0.1</version> 165 </dependency> 166 <!-- validator包含的类以及方法 --> 167 <dependency> 168 <groupId>org.hibernate</groupId> 169 <artifactId>hibernate-validator</artifactId> 170 <version>5.2.4.Final</version> 171 </dependency> 172 </dependencies> 173 174 <build> 175 <finalName>ssm01</finalName> 176 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> 177 <plugins> 178 <plugin> 179 <artifactId>maven-clean-plugin</artifactId> 180 <version>3.1.0</version> 181 </plugin> 182 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> 183 <plugin> 184 <artifactId>maven-resources-plugin</artifactId> 185 <version>3.0.2</version> 186 </plugin> 187 <plugin> 188 <artifactId>maven-compiler-plugin</artifactId> 189 <version>3.8.0</version> 190 </plugin> 191 <plugin> 192 <artifactId>maven-surefire-plugin</artifactId> 193 <version>2.22.1</version> 194 </plugin> 195 <plugin> 196 <artifactId>maven-war-plugin</artifactId> 197 <version>3.2.2</version> 198 </plugin> 199 <plugin> 200 <artifactId>maven-install-plugin</artifactId> 201 <version>2.5.2</version> 202 </plugin> 203 <plugin> 204 <artifactId>maven-deploy-plugin</artifactId> 205 <version>2.8.2</version> 206 </plugin> 207 </plugins> 208 </pluginManagement> 209 </build> 210 </project>
作者:刘认真
-------------------------------------------
新人只求记录学习生活!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!