Mybatis学习笔记 —— 与spring整合后的原始Dao及Mapper代理方式的使用

一、整合思路

  • spring通过单例方式管理SqlSessionFactory。
  • spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession。(spring和mybatis整合自动完成)
  • 持久层的mapper都需要由spring进行管理。

二、整合环境

三、spring配置文件applicationContext.xml

在applicationContext.xml配置sqlSessionFactory和数据源


    <context:property-placeholder location="DataSource.properties">context:property-placeholder>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}">property>
        <property name="url" value="${jdbc.url}">property>
        <property name="username" value="${jdbc.username}">property>
        <property name="password" value="${jdbc.password}">property>
    bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="configLocation" value="mybatis/SqlMapConfig.xml" />
        
        <property name="dataSource" ref="dataSource" />
    bean>

四、操作的数据表

Mybatis学习笔记 —— 与spring整合后的原始Dao及Mapper代理方式的使用_第1张图片

五、原始Dao开发

1、po类Student
public class Student {
    private int id;
    private String name;
    private String sex;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", sex=" + sex + "]";
    }

}
2、StuMapper.xml


<mapper namespace="cn.csdn.sm.dao.StuDao">
    <select id="findStuById" parameterType="int" resultType="cn.csdn.sm.po.Student">
        SELECT * FROM Student WHERE id=#{value}
    select>

    <select id="findStuByName" parameterType="String" resultType="cn.csdn.sm.po.Student">
         SELECT * FROM Student WHERE name LIKE '%${value}%'
    select>

    <insert id="addStu" parameterType="cn.csdn.sm.po.Student" >
        INSERT INTO Student(name,sex) VALUE(#{name},#{sex})
    insert>

    <delete id="delStu" parameterType="int">
        DELETE FROM Student WHERE id = #{value}
    delete>

    <update id="upStu" parameterType="cn.csdn.sm.po.Student">
        UPDATE  Student SET name = #{name}, sex = #{sex} where id = #{id}
    update>
mapper>
3、DAO接口
public interface StuDao {
    public Student findStuById(int id) throws Exception;

    public List findStuByName(String name) throws Exception;

    public void addStu(Student Stu) throws Exception;

    public void delStu(int id) throws Exception;

    public void upStu(Student Stu) throws Exception;
}
4、DAO实现类(继承SqlSessionDaoSupport)

注:
- 不在使用构造的方式注入sqlSessionFactory,而是通过spring注入,在实现类里面要继承spring-maybatis整合包中的SqlSessionDaoSupport类。
- 通过this.getSqlSession()获得sqlSession对象
- 不用close

public class StuImp extends SqlSessionDaoSupport implements StuDao{

    @Override
    public Student findStuById(int id) throws Exception {
        SqlSession sqlSession = this.getSqlSession();
        Student Stu = sqlSession.selectOne("cn.csdn.mybatis.dao.StuDao.findStuById",id);
        return Stu;
    }
    @Override
    public List findStuByName(String name) throws Exception {
        SqlSession sqlSession = this.getSqlSession();
        List Stus = sqlSession.selectList("cn.csdn.mybatis.dao.StuDao.findStuByName",name);
        return Stus;
    }
    @Override
    public void addStu(Student stu) throws Exception {
        SqlSession sqlSession = this.getSqlSession();
        sqlSession.insert("cn.csdn.mybatis.dao.StuDao.addStu",stu);
        sqlSession.commit();
    }
    @Override
    public void delStu(int id) throws Exception {
        SqlSession sqlSession = this.getSqlSession();
        sqlSession.delete("cn.csdn.mybatis.dao.StuDao.delStu",id);
        sqlSession.commit();
    }
    @Override
    public void upStu(Student stu) throws Exception {
        SqlSession sqlSession = this.getSqlSession();
        sqlSession.update("cn.csdn.mybatis.dao.StuDao.upStu",stu);
        sqlSession.commit();
    }

}
5、配置Dao

在applicationContext.xml中配置dao


    <bean id="stuDao" class="cn.csdn.sm.dao.StuImp">
        <property name="sqlSessionFactory" ref="sqlSessionFactory">property>
    bean>

在SqlMapConfig.xml中配置dao

<mappers>
      <mapper resource="mapper/StuMapper.xml"/>
mappers>
6、测试
class StuImpTest {
    private ApplicationContext applicationContext;

    @BeforeEach
    void setUp() throws Exception {
        applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
    }

    @Test
    void testFindStuById() throws Exception {
        StuDao stuDao = (StuDao) applicationContext.getBean("stuDao");
        Student student = stuDao.findStuById(1);
        System.out.println(student);

    }

}
7、步骤总结
  • 建立工程,导入jar包
  • spring和mybatis的配置文件
  • 在spring配置文件中
    • 配置数据源
    • 配置sqlSessionFactory(单例)
  • 写dao接口和实现类,实现类中继承SqlSessionDaoSupport
  • mybatis配置文件中加载mapper.xml
  • spring配置文件中配置dao实现类,并注入sqlSessionFactory

六、Mapper代理方式

1、创建StuMapper.java和StuMapper.xml

这里写图片描述

public interface StuMapper {
    public Student findStuById(int id) throws Exception;

    public List findStuByName(String name) throws Exception;

    public void addStu(Student Stu) throws Exception;

    public void delStu(int id) throws Exception;

    public void upStu(Student Stu) throws Exception;
}


<mapper namespace="cn.csdn.sm.mapper.StuMapper">
    <select id="findStuById" parameterType="int" resultType="cn.csdn.sm.po.Student">
        SELECT * FROM Student WHERE id=#{value}
    select>

    <select id="findStuByName" parameterType="String" resultType="cn.csdn.sm.po.Student">
         SELECT * FROM Student WHERE name LIKE '%${value}%'
    select>

    <insert id="addStu" parameterType="cn.csdn.sm.po.Student" >
        INSERT INTO Student(name,sex) VALUE(#{name},#{sex})
    insert>

    <delete id="delStu" parameterType="int">
        DELETE FROM Student WHERE id = #{value}
    delete>

    <update id="upStu" parameterType="cn.csdn.sm.po.Student">
        UPDATE  Student SET name = #{name}, sex = #{sex} where id = #{id}
    update>
mapper>
2、spring配置

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="basePackage" value="cn.csdn.ssm.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

    bean>

此时,SqlMapConfig.xml中也不用扫描dao包了

3、测试
class StuMapperTest {
    private ApplicationContext applicationContext;
    @BeforeEach
    void setUp() throws Exception {
        applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
    }

    @Test
    void testFindStuById() throws Exception {
        StuMapper stuMapper = (StuMapper) applicationContext.getBean("stuMapper");
        Student student = stuMapper.findStuById(1);
        System.out.println(student);
    }

}
4、步骤总结
  • 建立工程,导入jar包
  • spring和mybatis的配置文件
  • 在spring配置文件中
    • 配置数据源
    • 配置sqlSessionFactory(单例)
    • 配置mapper的批量扫描,扫描某包下的mapper,自动创建mapper代理对象并在spring中注册
  • 按照规范在上述包中写mapper.java和mapper.xml
  • 通过spring中applicationContext.getBean(String className)获得对象

你可能感兴趣的:(Java框架)