mybatis spring整合 SqlSessionTemplate类使用

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

在applicationContext.xml 中可以通过SqlSessionFactory作为参数来构建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 selectStu();
}

dao层实现类

提供setSqlSession方法

public class StudentDaoImpl implements StudentDao {
    private SqlSessionTemplate sqlSession;

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

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

applicationContext.xml


<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>

    
    <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

执行一个查询



    <mapper namespace="com.cyh.mapper">
        <select id="selectAll" resultType="student">
            select * from student
        select>
    mapper>

整合后的mybatis-config.xml

主要声明mappre.xml文件的位置, 和类型别名



<configuration>
    
    <typeAliases>
        <typeAlias type="pojo.Student" alias="student">typeAlias>
        typeAliases>

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

configuration>

你可能感兴趣的:(【MyBatis】,【Spring】)