Spring整合mybatis

spring整合mybatis

1.导入相关jar包

  • junit
  • mybatis
  • mysql数据库
  • spring相关的
  • aop织入
  • mybatis-spring[new]
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.20</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.5</version>
</dependency>

2.编写配置文件

1.编写数据源配置

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;
            useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC"/>
    <property name="username" value="root"/>
    <property name="password" value="123456"/>
</bean>

2.sqlSessionFactory

<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!--绑定配置文件-->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <property name="mapperLocations" value="classpath:com/ngk/mapper/*xml"/>
</bean>

3.sqlSessionTemplate

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <!--只能使用构造器注入sqlSessionFactory,因为他没有setting方法-->
    <constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>

4.需要给接口加实现类

  • 方式一
public class UserMapperImpl implements UserMapper{
    //原来所有操作都使用Session来执行,现在都使用SqlSessionTemplate
    private SqlSessionTemplate sqlSession;
    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }
    public List<User> getUser() {
        return sqlSession.getMapper(UserMapper.class).getUser();
    }
}
  • 方式二

  • //继承SqlSessionDaoSupport可以直接获取sqlSession
    public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
        public List<User> getUser() {
            return getSqlSession().getMapper(UserMapper.class).getUser();
        }
    }

5.将自己写的实现类,注入到Spring中

//方式一
<bean id="userMapper" class="com.ngk.mapper.UserMapperImpl">
    <property name="sqlSession" ref="sqlSession"/>
</bean>
   
//方式二    
<bean id="userMapper2" class="com.ngk.mapper.UserMapperImpl2">
   <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

6.测试

// 方式一
 @Test
 public void test() throws IOException {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
     UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
     for (User user : userMapper.getUser()) {
         System.out.println(user);
     }
 }
 //方式二
 @Test
 public void test2() throws IOException {
     ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
     UserMapper userMapper = context.getBean("userMapper2", UserMapper.class);
     for (User user : userMapper.getUser()) {
         System.out.println(user);
     }
 }

3.事务管理

  • 声明式事务:AOP
  • 编程式事务:需要在代码中进行事务管理

1.为什么需要事务

  • 如果不配置事务,可能存在数据提交不一致的情况
  • 如果不在Spring中配置事务,就需要在代码中手动配置事务
  • 事务在开发中十分重要,涉及到数据的一致性和完整性问题,不容马虎!

2.事务在核心配置文件中的配置

 <!--配置声明式事务-->
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <constructor-arg name="dataSource" ref="dataSource" />
 </bean>
 <!--结合AOP实现事务的织入-->
 <!--配置事务通知-->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
     <!--给那些方法配置事务-->
     <!--配置事务的传播特性 propagation-->
     <tx:attributes>
         <tx:method name="add" propagation="REQUIRED"/>
         <tx:method name="delete" propagation="REQUIRED"/>
         <tx:method name="update" propagation="REQUIRED"/>
         <tx:method name="query" read-only="true"/>
         <tx:method name="*" propagation="REQUIRED"/>
     </tx:attributes>
 </tx:advice>
 <!--配置事务切入-->
 <aop:config>
     <aop:pointcut id="txPointCut" expression="execution(* com.ngk.mapper.*.*(..))"/>
     <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
 </aop:config>
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页