myBatis 基于javaBean配置

Posted jonalin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了myBatis 基于javaBean配置相关的知识,希望对你有一定的参考价值。

 

MyBatis的持久化解决方案是将用户从原始的JDBC访问中解放出来,用户只需要定义需要操作的SQL语句,

无须关注底层的JDBC操作,就可以以面向对象的方式来进行持久化层操作.底层数据库连接的获取,数据访问的实现,事务控制等都无须用户关心,

从而将应用层从底层的JDBC/JTA API抽取出来.通过配置文件管理JDBC连接,让MyBatis解决持久化的实现.在MyBatis中的常见对象有SqlSessionFactory和SqlSession.

 

 

依赖

 

技术图片
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>5.1.27</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
View Code

 

技术图片

use类

技术图片
public class User 
    private Long id;
    private String username;
    private String address;

    @Override
    public String toString() 
        return "User" +
                "id=" + id +
                ", username=‘" + username + ‘\\‘‘ +
                ", address=‘" + address + ‘\\‘‘ +
                ‘‘;
    

    public Long getId() 
        return id;
    

    public void setId(Long id) 
        this.id = id;
    

    public String getUsername() 
        return username;
    

    public void setUsername(String username) 
        this.username = username;
    

    public String getAddress() 
        return address;
    

    public void setAddress(String address) 
        this.address = address;
    
View Code

DataSourceConfig 数据源配置类
技术图片
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


@Configuration
public class DataSourceConfig 
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.one")
    DataSource dsOne() 
        return DruidDataSourceBuilder.create().build();
    

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.two")
    DataSource dsTwo() 
        return DruidDataSourceBuilder.create().build();
    
View Code

 

MyBatisConfigOne
技术图片
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


@Configuration
@MapperScan(basePackages = "org.javaboy.mybatis.mapper1", sqlSessionFactoryRef = "sqlSessionFactory1", sqlSessionTemplateRef = "sqlSessionTemplate1")
public class MyBatisConfigOne 
    @Autowired
    @Qualifier("dsOne")
    DataSource dsOne;
    @Bean
    SqlSessionFactory sqlSessionFactory1() 
        SqlSessionFactory sessionFactory = null;
        try 
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dsOne);
            sessionFactory = bean.getObject();
         catch (Exception e) 
            e.printStackTrace();
        
        return sessionFactory;
    
    @Bean
    SqlSessionTemplate sqlSessionTemplate1() 
        return new SqlSessionTemplate(sqlSessionFactory1());
    
View Code
MyBatisConfigTwo
技术图片
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


@Configuration
@MapperScan(basePackages = "org.javaboy.mybatis.mapper2", sqlSessionFactoryRef = "sqlSessionFactory2", sqlSessionTemplateRef = "sqlSessionTemplate2")
public class MyBatisConfigTwo 
    @Autowired
    @Qualifier("dsTwo")
    DataSource dsOne;
    @Bean
    SqlSessionFactory sqlSessionFactory2() 
        SqlSessionFactory sessionFactory = null;
        try 
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dsOne);
            sessionFactory = bean.getObject();
         catch (Exception e) 
            e.printStackTrace();
        
        return sessionFactory;
    
    @Bean
    SqlSessionTemplate sqlSessionTemplate2() 
        return new SqlSessionTemplate(sqlSessionFactory2());
    
View Code
UserMapper1
技术图片
import org.javaboy.mybatis.bean.User;

import java.util.List;


//@Mapper
public interface UserMapper1 
//    @Select("select * from t_user")
    List<User> getAllUser();
View Code
UserMapper1.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="org.javaboy.mybatis.mapper1.UserMapper1">
    <select id="getAllUser" resultType="org.javaboy.mybatis.bean.User">
        select * from t_user;
    </select>
</mapper>
View Code

UserMapper2
技术图片
import org.javaboy.mybatis.bean.User;

import java.util.List;


//@Mapper
public interface UserMapper2
//    @Select("select * from t_user")
    List<User> getAllUser();
View Code
UserMapper2.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="org.javaboy.mybatis.mapper2.UserMapper2">
    <select id="getAllUser" resultType="org.javaboy.mybatis.bean.User">
        select * from t_user;
    </select>
</mapper>
View Code

application.properties 配置文件

技术图片
spring.datasource.one.url=jdbc:mysql:///test01
spring.datasource.one.username=root
spring.datasource.one.password=root
spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.one.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.two.url=jdbc:mysql:///test02
spring.datasource.two.username=root
spring.datasource.two.password=root
spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.two.driver-class-name=com.mysql.jdbc.Driver
View Code

 


 

以上是关于myBatis 基于javaBean配置的主要内容,如果未能解决你的问题,请参考以下文章

mybatis入门

基于Springoot框架的网站后台设计定做

找基于JAVA的管理系统定做

SpringMvc mybatis 基于注解式的事务管理怎么配

在IOC中装配Bean

怎么找基于SSM的系统设计定做找我们