Spring + mybatis 整合

   

整合后,无需提交session.commit();

无需关闭session.close();

   

Log4j.properties // 日志记录

   

   

  1. log4j.rootLogger=DEBUG,Console
  2. log4j.appender.Console=org.apache.log4j.ConsoleAppender
  3. log4j.appender.Console.layout=org.apache.log4j.PatternLayout
  4. log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
  5. log4j.logger.org.apache=DEBUG

   

目录结构

Spring + mybatis 整合

   

一、mapper代理开发

   

  1. <!-- mapper开发 -->
  2.     <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
  3.         <property name="mapperInterface" value="cn.tri.sm.dao.UserDao"></property>
  4.         <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
  5. </bean>

   

二、Dao继承SqlSessionDaoSupport

   

1、整合jar包

   

  • <dependencies>
  •         <dependency><!--隐含beans\core\aop\expression-->
  •             <groupId>org.springframework</groupId>
  •             <artifactId>spring-context</artifactId>
  •             <version>4.3.20.RELEASE</version>
  •         </dependency>
  •         
  •           <dependency>
  •             <groupId>org.springframework</groupId>
  •             <artifactId>spring-context-support</artifactId>
  •             <version>4.3.20.RELEASE</version>
  •         </dependency>
  •         
  •   <!-- 事物spring-tx -->
  •     <dependency>
  •             <groupId>org.springframework</groupId>
  •             <artifactId>spring-tx</artifactId>
  •             <version>4.3.20.RELEASE</version>
  •         </dependency>
  •  
  •         <!-- spring 整合 mybaties  
  •         <dependency>
  •             <groupId>org.mybatis</groupId>
  •             <artifactId>mybatis-spring</artifactId>
  •             <version>1.3.2</version>
  •         </dependency>
  •         <dependency>
  •             <groupId>org.springframework</groupId>
  •             <artifactId>spring-jdbc</artifactId>
  •             <version>4.3.20.RELEASE</version>
  •         </dependency>
  •         
  •         <!-- mysql jdbc -->
  •                        <dependency>
  •                 <groupId>org.testcontainers</groupId>
  •                 <artifactId>mysql</artifactId>
  •                 <version>1.11.1</version>
  •                 <scope>test</scope>
  •         </dependency>
  •         
  •         <dependency>
  •             <groupId>mysql</groupId>
  •             <artifactId>mysql-connector-java</artifactId>
  •             <version>5.1.6</version>
  •         </dependency>
  •         
  •         <dependency>
  •             <groupId>org.mybatis</groupId>
  •             <artifactId>mybatis</artifactId>
  •             <version>3.4.6</version>
  •         </dependency>
  •         
  •         <!-- commons包 -->
  •         <dependency>
  •             <groupId>commons-pool</groupId>
  •             <artifactId>commons-pool</artifactId>
  •             <version>1.6</version>
  •         </dependency>
  •         
  •         <!-- 数据源 -->
  •         <dependency>
  •             <groupId>commons-dbcp</groupId>
  •             <artifactId>commons-dbcp</artifactId>
  •             <version>1.4</version>
  •         </dependency>

       

  •         <!--日志-->
  •         <dependency>
  •             <groupId>log4j</groupId>
  •             <artifactId>log4j</artifactId>
  •             <version>1.2.17</version>
  •         </dependency>
  •   </dependencies>

   

2、配置文件

applicationContext.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"
  •     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

       

  •     <!-- 数据源 -->
  •     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  •         <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  •         <property name="url" value="jdbc:mysql://localhost:3306/project" />
  •         <property name="username" value="root" />
  •         <property name="password" value="admin" />
  •     </bean>
  •     
  •     <!-- 拿到sqlSessionFactory>>mybatis-spring.jar提供 -->
  •     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  •         <property name="dataSource" ref="dataSource"/>
  •         <!-- 读取mybatis配置文件-->
  •         <!-- <property name="configLocation" value="classpath:mybaties-config.xml"></property> -->
  •         <!-- 扫描mapper配置文件-->
  •         <property name="mapperLocations" value="classpath:cn\tri\sm\mapper\UserDao.xml"></property>
  •         <!-- 取别名-->
  •         <property name="typeAliasesPackage" value="cn.tri.sm.pojo"></property>
  •     </bean>
  •     
  •     <bean id="userDao" class="cn.tri.sm.daoImpl.UserDaoImpl">
  •         <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
  •     </bean>
  •     
  •     <bean id="userService" class="cn.tri.sm.serviceImpl.UserServiceImpl">
  •         <property name="userDao" ref="userDao"></property>
  •     </bean>
  • </beans>

   

   

Mybatis-config.xml

   

  • <?xml version="1.0" encoding="UTF-8" ?>
  • <!DOCTYPE configuration
  •   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  •   "http://mybatis.org/dtd/mybatis-3-config.dtd">
  • <configuration>
  •     <!-- 取别名 -->
  •     <typeAliases>
  •            <package name="cn.tri.sm.pojo"/>    
  •           <!--  <package name="cn.tri.sm.dao"/> -->
  •            <package name="cn.tri.sm.daoImpl"/>
  •     </typeAliases>
  •   <!-- 扫描包路径 -->
  •   <mappers>
  •   ????????<mapper resource="cn\tri\sm\mapper\UserDao.xml"/>
  •   </mappers>
  • </configuration>

   

3、demo

daoImpl

   

  1.     public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{
  2.     
  3.         public void addUser(User user) {
  4.             SqlSession session = super.getSqlSession();
  5.             session.insert("UserDao.addUser", user);
  6.             System.out.println("添加成功");
  7.         }
  8.     }

   

serviceImpl

   

  •     public class UserServiceImpl implements UserService{
  •         private UserDao userDao;
  •         
  •         public void setUserDao(UserDao userDao) {
  •             this.userDao = userDao;
  •         }
  •     
  •         public void addUser(User user) {
  •             userDao.addUser(user);
  •         }
  •     }

   

Servlet

   

  1.     public class TestAddUser {
  2.         public static void main(String[] args) {
  3.             ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  4.             UserService us = (UserService) context.getBean("userService");
  5.             User u = new User(2, "yefan", "888888");
  6.             us.addUser(u);
  7.         }
  8.     }

   

   

三、

<!-- mapper开发,第三方式。 交给MapperScannerConfigurer 类,批量扫描mapper接口,

注意name="sqlSessionFactoryBeanName" ,且此时为value="sqlSessionFactory",而不是ref

扫描到的mapper接口,springIOC管理后,id="首字母小写接口名"

   

  1.     <bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  2.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  3.         <property name="basePackage" value="cn.tri.sm.dao"></property>
  4.     </bean>
  5.     
  6.     <bean id="userService" class="cn.tri.sm.serviceImpl.UserServiceImpl">
  7.         <property name="userDao" ref="userDao"></property>
  8. </bean>

  

上一篇:2、IOC思想


下一篇:Spring--->声明式事务