Spring_整合Mybatis

1、导入相关jar包

  • junit

  • mybatis

  • mysq数据库

  • Spring相关的

  • aop织入

  • mybatis-spring 【new】

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.16</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

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


<!-- Spring架构   免费的开源的容器  轻量级的、非入侵式的 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.0.RELEASE</version>
</dependency>


<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.0.RELEASE</version>
</dependency>


<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4</version>
</dependency>

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

2、编写配置文件

3、测试

回忆Mybatis

  1. 编写实体类

  2. 编写核心配置文件

  3. 编写接口

  4. 编写Mapper.xml

  5. 测试

Mybatis-Spring

  1. 编写数据源配置

  2. sqlSessionFactory

  3. sqlSessionTemplate

    (前三步都是配置文件)

    ApplicationConte.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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
    <import resource="spring-dao.xml"/>


</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核心配置文件-->
<configuration>

    <typeAliases>
        <typeAlias type="com.ly.pojo.User" alias="User"/>
    </typeAliases>



    <!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册-->


</configuration>

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


    <!--DataSource:使用Spring的数据源替换mybatis的配置 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatistext?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=GMT"/>
        <property name="username" value="root"/>
        <property name="password" value="ly0825"/>
    </bean>
    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--绑定mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/ly/mapper/*.xml"/>
    </bean>
    <!--SqlSessionTemplate:就是我们使用的sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">

        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
    <bean id="userMapper" class="com.ly.mapper.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>

</beans>

4.需要给接口加实现类

UserMapperImpl.java

package com.ly.mapper;

import com.ly.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class UserMapperImpl implements UserMapper{
    //我们原来所有的操作,都是用sqlSession来执行,现在都使用SqlSessionTemplate
    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    public List<User> getUserList() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.getUserList();
    }
}

UserMapper.java(接口)

package com.ly.mapper;

import com.ly.pojo.User;

import java.util.List;

public interface UserMapper {
    //获取全部用户
    public  List<User> getUserList();
}

UserMapper.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">
<mapper namespace="com.ly.mapper.UserMapper">
<!--id里面是调用接口的方法,resultType是返回的结果类型-->
    <select id="getUserList" resultType="User">
        select * from user
    </select>
    <!--<insert id="insertUser" parameterType="User">
        insert  into user values(#{id},#{name},#{pwd})
    </insert>
    <update id="updateUser" parameterType="User">
        update user set `name` = #{name},pwd =#{pwd} where id = #{id}
    </update>
    <delete id="deleteUser" parameterType="int">
        delete from user where id=#{id}
    </delete>-->

</mapper>

User.java【实体类】

package com.ly.pojo;

import lombok.Data;

@Data
public class User {
    private int id;
    private String name;
    private String pwd;


}

5.将自己写的实现类注入到Spring中即可;

第二种整合方式:

package com.ly.mapper;

import com.ly.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import java.util.List;

//mybatis plus 精简版 不需要在配置文件中配置sqlSession,接口实现类不需要注入sqlSession
// 但是需要继承SqlSessionDaoSupport,需要注入sqlSessionFactory,将其交给父类,让其给我们生成sqlSession,我们只需要调用getSqlSession方法
public class UserMapperImplTwo extends SqlSessionDaoSupport implements UserMapper{


    public List<User> getUserList() {
        SqlSession sqlSession = getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();
        return userList;
    }
}

 

Spring_整合Mybatis

上一篇:STL容器之set容器API(二)无重复元素原理、set容器排序、自定义数据


下一篇:C++使用Jsoncpp源码