首页 > 编程语言 > 详细

Spring--->整合Mybatis

时间:2020-09-24 20:52:27      阅读:50      评论:0      收藏:0      [点我收藏+]

1、导入jar包

  • junit
<dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
</dependency>
  • mybayis
<dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis</artifactId>
   <version>3.5.2</version>
</dependency>
  • mysql
    • <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>5.1.47</version>
      </dependency>

       

  • Spring
    • <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc</artifactId>
         <version>5.1.10.RELEASE</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-jdbc</artifactId>
         <version>5.1.10.RELEASE</version>
      </dependency>
  • aspectj AOP
    • <dependency>
         <groupId>org.aspectj</groupId>
         <artifactId>aspectjweaver</artifactId>
         <version>1.9.4</version>
      </dependency>
  • Spring-Mybatis【NEW】
    • <dependency>
         <groupId>org.mybatis</groupId>
         <artifactId>mybatis-spring</artifactId>
         <version>2.0.2</version>
      </dependency>
  • 配置Maven静态资源过滤问题
<build>
   <resources>
       <resource>
           <directory>src/main/java</directory>
           <includes>
               <include>**/*.properties</include>
               <include>**/*.xml</include>
           </includes>
           <filtering>true</filtering>
       </resource>
   </resources>
</build>

整合

2、回忆Mybatis

User类

package com.xian.dao;

public class User {
    private int id;
    private String name;
    
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ‘}‘;
    }
}

UserMapper接口

package com.xian.Mapper;

import com.xian.dao.User;

import java.util.List;

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

UserMapper.xml(与接口同包同名)

package com.xian.Mapper;

        import com.xian.dao.User;

        import java.util.List;

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

Mybatis-config

<?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>
    <properties resource="db.properties">
    </properties>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${name}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
<mappers>
//class定位类的属性
<mapper class="com.xian.dao.User"/>
</mappers>
</configuration>

db.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC&useUnicode=ture&characterEncoding=UTF-8
name=root
password=123456

测试类代替Utils类直接测试

 public void selectUser() throws IOException {

        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        List<User> userList = mapper.getUser();
        for (User user: userList){
            System.out.println(user);
        }

        sqlSession.close();
    }

3、整合Spring—Mybatis(方式一)

 User类,UserMapper接口和UserMapper不变

Mybatis-config.xml极度简化(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>
    <typeAliases>
        <package name="com.xian.dao.User"/>
    </typeAliases>
</configuration>

复杂的是配置Bean

<?xml version="1.0" encoding="GBK"?>
<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
       http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--    引用一个数据源-->
    <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/testdb?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
<!--    创建SQLsessionFaction,引用上面的数据源-->
    <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/xian/Mapper/*.xml"/>
    </bean>
<!--    SqlSessionTemplate 需要一个sqlSessionFactory 通过构造器注入-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>

<!--    为数据源添加一个接口实现类,并注册bean、set注入一个sqlsesion-->
    <bean id="userDao" class="com.xian.dao.UserImpl">
        <property name="sqlSessionTemplate" ref="sqlSession" />
    </bean>
</beans>

 

多写一个接口实现类(注入SQLsession)

package com.xian.dao;

import com.xian.Mapper.UserMapper;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class UserImpl implements UserMapper {
    private SqlSessionTemplate sqlSessionTemplate;
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }
    public List<User> getUser() {
        UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
        return mapper.getUser();
    }
}

测试

public void Test02(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserImpl userDao = (UserImpl) context.getBean("userDao");
        List<User> user = userDao.getUser();
        System.out.println(user);
    }

整合Mybatis(方式二)

简化接口实现类

public class UserImpl extends SqlSessionDaoSupport implements UserMapper {
    public List<User> getUser() {
        UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
        return mapper.getUser();
    }
}

直接测试

public void Test02(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserImpl userDao = (UserImpl) context.getBean("userDao");
        List<User> user = userDao.getUser();
        System.out.println(user);
    }

整合完的Mybatis-config极度简化,自动托管后甚至可以完全省略

 

Spring--->整合Mybatis

原文:https://www.cnblogs.com/springxian/p/13725172.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!