spring-jdbc+mybatis实现示例

【README】

特别注意

  1. SqlSessionFactory是依赖SqlSessionFactoryBean创建;
  2. 业务Mapper是依赖MapperFactoryBean创建;

所以若要研究spring整合mybaits的源码,需要从SqlSessionFactoryBean,MapperFactoryBean入手;


【1】spring-jdbc开发例子

【1.1】定义防腐层support 

public interface IUserSupport {

    void save(UserPO userPO);
    List<UserPO> getUsers();
}

public class UserSupportImpl implements IUserSupport {

    private JdbcTemplate jdbcTemplate;


    public UserSupportImpl(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public void save(UserPO userPO) {
        String userId = String.valueOf(System.currentTimeMillis());
        jdbcTemplate.update("insert into spring_user_tbl(id, name, age, sex) values(?,?,?,?)",
                new Object[]{userId, userPO.getName(), userPO.getAge(), userPO.getSex()}
                , new int[]{Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.VARCHAR});
    }

    @Override
    public List<UserPO> getUsers() {
        List<UserPO> list = jdbcTemplate.query("select * from spring_user_tbl", new UserRowMapper());
        return list;
    }
}


【1.2】定义dao层

【UserPO】

@Data
@NoArgsConstructor
public class UserPO {
    private String id;
    private String name;
    private int age;
    private String sex;

    public UserPO(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public UserPO(String id, String name, int age, String sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
}

【UserRowMapper】

public class UserRowMapper implements RowMapper {
    @Override
    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        UserPO person = new UserPO(rs.getString("id"), rs.getString("name"), rs.getInt("age"), rs.getString("sex"));
        return person;
    }
}

【1.3】spring-beans.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"
       xmlns:context="http://www.springframework.org/schema/context"
       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-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/aop/spring-context-3.0.xsd
    ">

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/myspring" />
        <property name="username" value="root" />
        <property name="password" value="rootroot" />
        <property name="initialSize" value="2" />
        <property name="maxIdle" value="2" />
        <property name="minIdle" value="1" />
    </bean>

    <bean id="userService" class="com.tom.srccode.deep.analysis.chapter8.springjdbc.UserSupportImpl">
       <constructor-arg ref="dataSource"/>
    </bean>

</beans>

【1.4】测试用例

@Slf4j
public class SpringJdbcMain {

    public static void main(String[] args) {
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("/a_src_deep_analysis/chapter08/beans0801.xml");

        UserSupportImpl userSupport = applicationContext.getBean("userService", UserSupportImpl.class);
        UserPO userPO = new UserPO("lisi", 14, "男");
        userSupport.save(userPO);

        // 查询数据
        List<UserPO> users = userSupport.getUsers();
        users.forEach(System.out::println);
    }
}

【打印结果】

org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3930015a
org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 2 bean definitions from class path resource [a_src_deep_analysis/chapter08/beans0801.xml]
org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'dataSource'
org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userService'

org.springframework.jdbc.core.JdbcTemplate - Executing prepared SQL update
org.springframework.jdbc.core.JdbcTemplate - Executing prepared SQL statement [insert into spring_user_tbl(id, name, age, sex) values(?,?,?,?)]

org.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
org.springframework.jdbc.core.JdbcTemplate - Executing SQL query [select * from spring_user_tbl]
org.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource


UserPO(id=1001, name=zhangsan, age=10, sex=男)
UserPO(id=1782651335094, name=zhangsan, age=10, sex=男)
UserPO(id=1782651354529, name=zhangsan, age=10, sex=男)
UserPO(id=1782651362859, name=zhangsan, age=10, sex=男)
UserPO(id=1783116902846, name=lisi, age=14, sex=男)
UserPO(id=3, name=张三03, age=33, sex=男)

【2】spring-mybaits开发例子

【2.1】定义dao层

@Data
@NoArgsConstructor
public class UserPO {
    private String id;
    private String name;
    private int age;
    private String sex;

    public UserPO(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public UserPO(String id, String name, int age, String sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
}

// UserMapper
public interface UserMapper {
    void insertUser(UserPO user);
    UserPO getUser(String id);
}

【2.2】定义Mybatis工具 

@Slf4j
public class MybatisUtils {

    @Getter
    private final static SqlSessionFactory sqlSessionFactory;

    static {
        String resource = "a_src_deep_analysis/chapter9/mybatis-config.xml";
        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader(resource);
        } catch (Exception e) {
            log.error("读取 mybatis-config.xml文件异常", e);
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    }
}

【2.2.1】mybaits-config.xml 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
        <setting name="cacheEnabled" value="true"/>
        <setting name="defaultExecutorType" value="SIMPLE"/>
    </settings>

    <typeAliases>
        <typeAlias alias="UserPO" type="com.tom.srccode.deep.analysis.chapter9.UserPO" />
    </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/myspring"/>
                <property name="username" value="root"/>
                <property name="password" value="rootroot"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="a_src_deep_analysis/chapter9/UserMapper.xml" />
    </mappers>

</configuration>

【2.2.2】UserMapper

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.tom.srccode.deep.analysis.chapter9.UserMapper">

    <insert id="insertUser" parameterType="UserPO">
        INSERT INTO spring_user_tbl
        (id, name, age, sex)
        VALUES(#{id}, #{name}, #{age}, #{sex})
    </insert>

    <select id="getUser" resultType="UserPO" parameterType="string">
        select * from spring_user_tbl where id=#{id}
    </select>

</mapper>

【2.3】测试用例

public class Chapter9UnitTest {

    static SqlSessionFactory sqlSessionFactory = MybatisUtils.getSqlSessionFactory();

    @Test
    public void testAdd() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            UserPO userPO = new UserPO("4", "张三04", 44, "男");
            mapper.insertUser(userPO);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
    }

    @Test
    public void testGet() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            UserPO userPO = mapper.getUser("4");
            log.info("userPO = {}", userPO);
        } catch (Exception e) {
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
    }
}

【2.3.1】测试新增

DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection
DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 836969741.
DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@31e3250d]
DEBUG com.tom.srccode.deep.analysis.chapter9.UserMapper.insertUser - ==>  Preparing: INSERT INTO spring_user_tbl (id, name, age, sex) VALUES(?, ?, ?, ?)
DEBUG com.tom.srccode.deep.analysis.chapter9.UserMapper.insertUser - ==> Parameters: 4(String), 张三04(String), 44(Integer), 男(String)
DEBUG com.tom.srccode.deep.analysis.chapter9.UserMapper.insertUser - <==    Updates: 1
DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@31e3250d]
DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@31e3250d]
DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@31e3250d]
DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 836969741 to pool.

【2.3.2】测试查询

DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection
DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 1220759559.
DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@48c35007]
DEBUG com.tom.srccode.deep.analysis.chapter9.UserMapper.getUser - ==>  Preparing: select * from spring_user_tbl where id=?
DEBUG com.tom.srccode.deep.analysis.chapter9.UserMapper.getUser - ==> Parameters: 4(String)
DEBUG com.tom.srccode.deep.analysis.chapter9.UserMapper.getUser - <==      Total: 1
INFO com.tom.srccode.deep.analysis.chapter9.Chapter9UnitTest - userPO = UserPO(id=4, name=张三04, age=44, sex=男)
DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@48c35007]
DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@48c35007]
DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 1220759559 to pool.

【2.4】spring与mybatis整合例子

【2.4.1】spring与mybatis整合配置xml

 【spring-mybatis-beans.xml】 

  1. 配置了SqlSessionFactoryBean,用于注入SqlSessionFactory;
  2. 配置了 MapperFactoryBean,用于注入 业务Mapper,如UserMapper;
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/myspring" />
        <property name="username" value="root" />
        <property name="password" value="rootroot" />
        <property name="initialSize" value="2" />
        <property name="maxIdle" value="2" />
        <property name="minIdle" value="1" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:a_src_deep_analysis/chapter9/mybatis-config.xml"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.tom.srccode.deep.analysis.chapter9.UserMapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>

</beans>

【spring-mybatis-config.xml 】

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
        <setting name="cacheEnabled" value="true"/>
        <setting name="defaultExecutorType" value="SIMPLE"/>
    </settings>

    <typeAliases>
        <typeAlias alias="UserPO" type="com.tom.srccode.deep.analysis.chapter9.UserPO" />
    </typeAliases>

    <mappers>
        <mapper resource="a_src_deep_analysis/chapter9/UserMapper.xml" />
    </mappers>

</configuration>

【2.4.2】测试用例

@Slf4j
public class SpringMybatisIntegrateUnitTest {
    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("a_src_deep_analysis/chapter9/spring-mybatis-beans.xml");
        UserMapper userMapper = context.getBean(UserMapper.class);
        log.info("user = {}", userMapper.getUser("3"));
    }
}

【2.4.3】打印结果

DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e4a7dd4
DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 3 bean definitions from class path resource [a_src_deep_analysis/chapter9/spring-mybatis-beans.xml]
DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'dataSource'
DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'sqlSessionFactory'
DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
DEBUG org.mybatis.spring.SqlSessionFactoryBean - Parsed configuration file: 'class path resource [a_src_deep_analysis/chapter9/mybatis-config.xml]'
DEBUG org.mybatis.spring.SqlSessionFactoryBean - Property 'mapperLocations' was not specified.
DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userMapper'
DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6f2cb653] was not registered for synchronization because synchronization is not active
DEBUG org.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
[Finalizer] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
DEBUG org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [858727880, URL=jdbc:mysql://localhost:3306/myspring, MySQL Connector/J] will not be managed by Spring
DEBUG com.tom.srccode.deep.analysis.chapter9.UserMapper.getUser - ==>  Preparing: select * from spring_user_tbl where id=?
DEBUG com.tom.srccode.deep.analysis.chapter9.UserMapper.getUser - ==> Parameters: 3(String)
DEBUG com.tom.srccode.deep.analysis.chapter9.UserMapper.getUser - <==      Total: 1
DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6f2cb653]
INFO com.tom.srccode.deep.analysis.chapter9.SpringMybatisIntegrateUnitTest - user = UserPO(id=3, name=张三03, age=33, sex=男)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值