整合Mybatis方式一

HeJin大约 3 分钟Spring全家桶Spring入门

编写数据源配置

<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/hello?serverTimezone=UTC&amp;useSSL=True"/>
    <property name="username" value="root"/>
    <property name="password" value="****"/>
</bean>

sqlSessionFactory

<!--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/kuang/mapper/*.xml"/>
</bean>

sqlSessionFactoryTemplate

<!--SqlSessionTemplate:就是我们使用的SqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <!--只能使用构造器注入,因为它没有set方法-->
    <constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>

完整配置: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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--DataSource:使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid
        我们这里使用Spring提供的JDBC
    -->
    <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/hello?serverTimezone=UTC&amp;useSSL=True"/>
        <property name="username" value="root"/>
        <property name="password" value="13518529311"/>
    </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/kuang/mapper/*.xml"/>
    </bean>
    <!--SqlSessionTemplate:就是我们使用的SqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <!--只能使用构造器注入,因为它没有set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

</beans>

需要给接口加实现类

PopulationMapper.java

package com.kuang.mapper;

import com.kuang.pojo.Population;

import java.util.List;

/**
 * @author Administrator
 */
public interface PopulationMapper {
    /**
     * 查询用户
     * @return
     */
    public List<Population> selectPopulation();
}

PopulationMapper.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.kuang.mapper.PopulationMapper">

    <select id="selectPopulation" resultType="population">
        select * from hello.pop_numbers;
    </select>

</mapper>

PopulationMapperImpl.java

package com.kuang.mapper;

import com.kuang.pojo.Population;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

/**
 * @author Administrator
 */
public class PopulationMapperImpl implements PopulationMapper {
    /**
     * 在原来,我们的所有操作,都是以sqlSession来执行。现在都是使用SqlSessionTemplate
     * @return
     */

    private SqlSessionTemplate sqlSessionTemplate;

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    public List<Population> selectPopulation() {
        PopulationMapper mapper = sqlSessionTemplate.getMapper(PopulationMapper.class);
        return mapper.selectPopulation();
    }
}

实现类注入到Spring中

<bean id="populationMapper" class="com.kuang.mapper.PopulationMapperImpl">
    <property name="sqlSessionTemplate" ref="sqlSession"/>
</bean>

完整配置: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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <import resource="spring-dao.xml"/>

    <bean id="populationMapper" class="com.kuang.mapper.PopulationMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"/>
    </bean>

</beans>

测试使用

@Test
public void test2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    PopulationMapper populationMapper = context.getBean("populationMapper", PopulationMapper.class);

    for (Population population : populationMapper.selectPopulation()) {
        System.out.println(population);
    }

}

结果

Population(city=全    国, total=1332810869, male=682329104, female=650481765)
Population(city=北    京, total=19612368, male=10126430, female=9485938)
Population(city=天    津, total=12938693, male=6907091, female=6031602)
Population(city=河    北, total=71854210, male=36430286, female=35423924)
Population(city=山    西, total=35712101, male=18338760, female=17373341)
Population(city=内 蒙 古, total=24706291, male=12838243, female=11868048)
Population(city=辽    宁, total=43746323, male=22147745, female=21598578)
Population(city=吉    林, total=27452815, male=13907218, female=13545597)
Population(city=黑 龙 江, total=38313991, male=19426106, female=18887885)
Population(city=上    海, total=23019196, male=11854916, female=11164280)
Population(city=江    苏, total=78660941, male=39626707, female=39034234)
Population(city=浙    江, total=54426891, male=27965641, female=26461250)
Population(city=安    徽, total=59500468, male=30245513, female=29254955)
Population(city=福    建, total=36894217, male=18981054, female=17913163)
Population(city=江    西, total=44567797, male=23003521, female=21564276)
Population(city=山    东, total=95792719, male=48446944, female=47345775)
Population(city=河    南, total=94029939, male=47493063, female=46536876)
Population(city=湖    北, total=57237727, male=29391247, female=27846480)
Population(city=湖    南, total=65700762, male=33776459, female=31924303)
Population(city=广    东, total=104320459, male=54400538, female=49919921)
Population(city=广    西, total=46023761, male=23924704, female=22099057)
Population(city=海    南, total=8671485, male=4592283, female=4079202)
Population(city=重    庆, total=28846170, male=14608870, female=14237300)
Population(city=四    川, total=80417528, male=40827834, female=39589694)
Population(city=贵    州, total=34748556, male=17905471, female=16843085)
Population(city=云    南, total=45966766, male=23856696, female=22110070)
Population(city=西    藏, total=3002165, male=1542652, female=1459513)
Population(city=陕    西, total=37327379, male=19287575, female=18039804)
Population(city=甘    肃, total=25575263, male=13064193, female=12511070)
Population(city=青    海, total=5626723, male=2913793, female=2712930)
Population(city=宁    夏, total=6301350, male=3227404, female=3073946)
Population(city=新    疆, total=21815815, male=11270147, female=10545668)