Spring整理_06_整合MyBatis

1.步骤:

  1.引入依赖

    • junit
    • mybatis
    • mysql
    • spring相关
    • aop织入
    • mybatis-spring
    • lombok:可有可无
<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>

        <!--spring操作数据库:spring-jdbc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>
    </dependencies>

     2.编写配置文件

  3.测试

2.回忆MyBatis

  2.1编写实体类

  2.2编写mybatis配置文件

<?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核心配置文件-->
<configuration>
    <typeAliases>
        <package name="com.jiabowen.pojo"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybaits"/>
                <property name="username" value="root"/>
                <property name="password" value="1234"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper class="com.jiabowen.mapper.UserMapper"></mapper>
    </mappers>

</configuration>

 

  2.3编写接口

  2.4Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace:绑定接口,实现接口中的方法-->
<mapper namespace="com.jiabowen.mapper.UserMapper">
    <insert id="addUser" parameterType="com.jiabowen.pojo.User">
        insert into user (`name`,psw) values (#{name},#{psw});
    </insert>
</mapper>

 

  2.5测试

public class MyTest {
    @Test
    public void test1(){

        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = new User();
        user.setId(111);
        user.setName("333");
        user.setPsw("zzz333sss");

        int i = mapper.addUser(user);
        System.out.println(i);

        sqlSession.commit();//!!!
        sqlSession.close();//!!!
    }
}

 

可能出现问题:

1.增删改注意设置自动提交或手动提交

sqlSession.commit();//!!!
sqlSession.close();//!!!

 

或(自动提交)

sqlSessionFactory.openSession(true);

 

2.静态资源导出问题(这个忘了可太致命了!)

<!--静态资源导出问题-->
<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>

 

3.Mybatis-Spring学习

  3.1方法一:

  1. 引入jar包
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.2</version>
    </dependency> 
  2. spring配置文件
    • dataSoure:使用Spring的数据源代替mybatis的配置
      <!--配置数据源-->
      <bean id="dataSoure" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
          <property name="url" value="jdbc:mysql://localhost:3306/mybaits"/>
          <property name="username" value="root"/>
          <property name="password" value="1234"/>
      </bean>
    • sqlSessionFactroy
      <!--sqlSessionFactory-->
      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSoure"></property>
      <!--绑定mybatis配置文件-->
      <property name="configLocation" value="classpath:mybatis-config.xml"></property>
      <!--注册映射器!!!!-->
      <property name="mapperLocations" value="classpath:com/jiabowen/mapper/*.xml"></property>
      </bean>
    • sqlSession
      <!--sqlSession-->
      <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
          <!--因为SqlSessionTemplate这个类没有set方法,所以使用有参构造赋值(源码)-->
          <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
      </bean>
  3. 创建实现类:UserMapperImpl.java

    public class UserMapperImpl implements UserMapper {
    
        private SqlSessionTemplate sqlSession;
    
        public void setSqlSession(SqlSessionTemplate sqlSession) {
            this.sqlSession = sqlSession;
        }
    
        public int addUser(User user) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            return mapper.addUser(user);
        }
  4. 在spring-dao中注册实现类
    <bean id="userMapper" class="com.jiabowen.mapper.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"></property>
    </bean>
  5. 测试:

 

思路整理:

  1. 编写数据源
  2. sqlSessionFactory:核心对象
  3. sqlSessionTemplate:核心对象(sqlSession)
  4. 接口加实现类:spring整合mybatis多了一个实现类,因为spring接管这个对象可以自动创建。而mybatis无法实现自动创建,所以需要实现类
  5. 将实现类注入到Spring中
  6. 测试使用

  3.2、方法二

继承SqlSessionDaoSupport,调用getSqlSession();

前面步骤类似,只是sqlSession由自己创建变成了由SqlSessionDaoSupport中getSqlSession()方法创建

(去掉sqlSession bean)

 

上一篇:MyBatis 简介


下一篇:mybatis学习