mybatis spring整合 SqlSessionTemplate類使用

SqlSessionTemplate 通過使用SqlSession接口來完成工作, 所以他也有selectOne等方法。

在applicationContext.xml 中可以通過SqlSessionFactory作爲參數來構建SqlSessionTemplate

    <!--創建sqlSessionTemplate -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" ></constructor-arg>
    </bean>

使用列子

創建pojo類

public class Student {

    private Integer stuId;
    private String stuName;
    private Integer stuAge;
    private String stuMajor;
    private Date birthday;
...
}

dao層接口

public interface StudentDao{
    public List<Student> selectStu();
}

dao層實現類

提供setSqlSession方法

public class StudentDaoImpl implements StudentDao {
    private SqlSessionTemplate sqlSession;

    public List<Student> selectStu() {
        return sqlSession.selectList("com.cyh.mapper.selectAll");
    }

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }
}

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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- 加載數據庫參數 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driverClassName}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>

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

    <!--創建sqlSessionTemplate -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" ></constructor-arg>
    </bean>


    <bean id="studentDao" class="impl.StudentDaoImpl">
        <property name="sqlSession" ref="sqlSessionTemplate" />
    </bean>

</beans>

其中jdbc.properties如下

studentMapper.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="com.cyh.mapper">
        <select id="selectAll" resultType="student">
            select * from student
        </select>
    </mapper>

整合後的mybatis-config.xml

主要聲明mappre.xml文件的位置, 和類型別名

<?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="jdbc.properties" />-->
    <typeAliases>
        <typeAlias type="pojo.Student" alias="student"></typeAlias>
        </typeAliases>

    <mappers>
    <mapper resource="StudentMapper.xml" />
    </mappers>

</configuration>
發佈了684 篇原創文章 · 獲贊 241 · 訪問量 72萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章