通过spring集成mybatis,可以更快速方便的搭建与数据库链接的框架。与上一章节相比,减少了mybatisCfg.xml文件,减少了mybatisUtil.java工具类,更简单的添加事务管理,这些都在spring的配置文件中实现。
1、pom.xml文件增加依赖包:
<!--propertiesUtil 读取配置文件--> <dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>1.10</version> </dependency>
<properties> <spring.version>4.0.5.RELEASE</spring.version> </properties> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> <!-- mybatis驱动包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!--mybatis-spring适配器 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!--Spring java数据库访问包,在本例中主要用于提供数据源 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!-- 为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理。 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency>
2、配置数据源db.properties
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/mytestdatabase?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 username=qiaozhong password=1
3、编写spring配置文件spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:mybatisConfig/db.properties" /> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapping.xml文件,**表示迭代查找 --> <property name="mapperLocations" value="classpath:mybatis/**.xml" /> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 ,包下的类需要使用@MapperScan注解,否则容器注入会失败 --> <bean id="mapperScanConfig" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="mybatis.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
4、编写DO文件User.java
package mybatis.DO; public class User { private Long id; private String name; private String age; private String sex; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public String getAge() { return age; } public void setAge(String age) { this.age = age == null ? null : age.trim(); } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex == null ? null : sex.trim(); } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]"; } }
5、编写dao文件UserMapper.java,要用注解@MapperScan进行注解
package mybatis.dao; import java.util.List; import org.mybatis.spring.annotation.MapperScan; import mybatis.DO.User; @MapperScan public interface UserMapper { int deleteByPrimaryKey(Long id); int insert(User record); User selectByPrimaryKey(Long id); List<User> selectAll(); int updateByPrimaryKey(User record); }
6、编写测试类SpringMybatisTest.java
package mybatis.test; import java.util.List; import mybatis.DO.User; import mybatis.dao.UserMapper; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/springConfig/spring-mybatis.xml") public class SpringMybatisTest { @Autowired(required=false) @Qualifier("userMapper") private UserMapper userMapperDao; @Test @Transactional//开启事务管理 public void testConfigurationAnnotion(){ try { List<User> users = userMapperDao.selectAll(); for (int i = 0; i < users.size(); i++) { System.out.println(users.get(i)); } User user = new User(); user.setId(2L); user.setName("name1"); user.setAge("25"); user.setSex("male"); userMapperDao.insert(user); String nullString= null; System.out.println(nullString.equals("1")); User user2 = new User(); user2.setId(3L); user2.setName("name2"); user2.setAge("28"); user2.setSex("female"); userMapperDao.insert(user2); } catch (Exception e) { e.printStackTrace(); } } }
数据库表中原有一条数据:
测试结果:
User [id=1, name=qiaozhong, age=30, sex=male] java.lang.NullPointerException at mybatis.test.SpringMybatisTest.testConfigurationAnnotion(SpringMybatisTest.java:39) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 2019-11-18 21:29:53.956 [main] org.springframework.test.context.transaction.TransactionalTestExecutionListener INFO - Rolled back transaction after test execution for test context [DefaultTestContext@b684286 testClass = SpringMybatisTest, testInstance = mybatis.test.SpringMybatisTest@880ec60, testMethod = testConfigurationAnnotion@SpringMybatisTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@3f3afe78 testClass = SpringMybatisTest, locations = '{classpath:/springConfig/spring-mybatis.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]] 2019-11-18 21:29:53.959 [Thread-0] org.springframework.context.support.GenericApplicationContext INFO - Closing org.springframework.context.support.GenericApplicationContext@5f2050f6: startup date [Mon Nov 18 21:29:52 CST 2019]; root of context hierarchy
结果分析:
1、打印了User [id=1, name=qiaozhong, age=30, sex=male],说明已经链接到数据库查到数据。
2、执行后,数据库表中数据还是只有一条,说明测试方法中的两个insert语句都没有插入成功。因为在第一个插入语句成功插入表后,有代码抛异常,开启了事务管理之后,会将第一个插入语句回滚。
个人理解,如有错误,欢迎指正!