Spring知识小汇(8)——Spring整合Mybatis

整合Mybatis

步骤:

  1. 导包
    • junit
    • mysql
    • mybatis
    • mybatis-spring
    • spring-webMVC
  2. 编写配置文件
  3. 测试
回忆mybatis
  1. 编写实体类

    public class User {
        private int id;
        private String name;
        private int age;
        private String email;
    
        public User() {
        }
    
        public User(int id, String name, int age, String email) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.email = email;
        }
    
        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 int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    ", email='" + email + '\'' +
                    '}';
        }
    }
    
  2. 编写核心配置文件

    <?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.wjq.pojo"/>
        </typeAliases>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&amp;useSSL=true"/>
                    <property name="username" value="root"/>
                    <property name="password" value="123456"/>
                </dataSource>
            </environment>
        </environments>
    
        <mappers>
            <mapper class="com.wjq.mapper.UserMapper"/>
        </mappers>
    
    </configuration>
    
  3. 编写接口

    public interface UserMapper {
        public List<User> queryUser();
    }
    
  4. 编写Mapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.wjq.mapper.UserMapper">
        <select id="queryUser" resultType="user">
            select * from user
        </select>
    </mapper>
    
  5. 测试

    public class MyTest {
        public static void main(String[] args) throws IOException {
            String resource = "mybatis-config.xml";
            InputStream is = Resources.getResourceAsStream(resource);
    
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = factory.openSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            List<User> users = mapper.queryUser();
            for (User user : users) {
                System.out.println(user);
            }
            sqlSession.close();
    
        }
    }
    
整合Mybatis
  1. 编写spring-dao.xml获取数据源,获取SqlSessionFactory(从SqlSessionFactoryBean中获得),获取SqlSession(从SqlSessionTemplate中获得)

    <?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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!--    获取数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="password" value="123456"/>
        <property name="username" value="root"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&amp;useSSL=true"/>
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
    </bean>
    
    <!--    获取sqlSessionFactory通过SqlSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--    用到数据源-->
        <property name="dataSource" ref="dataSource"/>
    <!--    绑定Mybatis配置文件,之前写过,删除environments标签-->
        <property name="configLocation" value="mybatis-config.xml"/>
    <!--    <property name="mapperLocations" value="classpath:com/wjq/mapper/*.xml"/>-->
    </bean>
    
    <!--SqlSessionTemplate 就是我们所用到的SqlSession
    参数是SqlSessionFactory类型,public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory){}
    并且没有set()-->
        
        <!--方式二可以不用这步因为SqlSessionDaoSupport类里面会创建SqlSessionTemplate-->
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
            <constructor-arg index="0" ref="sqlSessionFactory"/>
        </bean>
    </beans>
    
  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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <import resource="spring-dao.xml"/>
        <bean id="userMapper" class="com.wjq.mapper.UserMapperImpl">
            <constructor-arg index="0" ref="sqlSession"/>
        </bean>
        
        
        <!--方式二的配置文件-->
        <!--    因为继承的类的set()的参数是SqlSessionFactory类型-->
        <!--<bean id="userMapper2" class="com.wjq.mapper.UserMapperImpl2">
            <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        </bean>-->
    </beans>
    
  3. 接口

    public interface UserMapper {
        public List<User> queryUser();
    }
    
  4. 实现类

    方式一

    public class UserMapperImpl implements UserMapper {
        //获得sqlSession
        private SqlSessionTemplate sqlSession;
    
        public UserMapperImpl(SqlSessionTemplate sqlSession) {
            this.sqlSession = sqlSession;
        }
    
        public List<User> queryUser() {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            return mapper.queryUser();
        }
    }
    

    方式二

    public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
        public List<User> queryUser() {
            return getSqlSession().getMapper(UserMapper.class).queryUser();
        }
    }
    
  5. 测试

public class MyTest {
    public static void main(String[] args) throws IOException {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
        List<User> users = userMapper.queryUser();
        for (User user : users) {
            System.out.println(user);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值