mybatis-spring整合之原始dao方法之记录

时间:2023-01-16 05:10:54

1、beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL --> <beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:context="http://www.springframework.org/schema/context"  xmlns:aop="http://www.springframework.org/schema/aop"  xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.0.xsd  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <!--外部参数的配置  引入jdbc配置文件  -->   <context:property-placeholder location="mybatisAndSpring/db.properties"/>
    <!--创建jdbc数据源 -->   <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxOpenPreparedStatements" value="10"/>
        <property name="minIdle" value="5"/>
    </bean>
    <!--创建sqlSessionFactory,同时指定数据源-->  <!-- mapper配置 -->  <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 -->  <property name="dataSource" ref="dataSource"/>
        <!-- 加载mybatis的全局配置文件 -->   <property name="configLocation" value="classpath:mybatisAndSpring/mybatisConfig/SqlMapConfig.xml"/>

    </bean>
    <!-- 配置DAO -->   <bean id="personDao" class="mybatis.mybatisAndSpring.dao.PersonDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>  </beans>

2、sqlMapperConfig.xml

<?xml version="1.0" encoding="utf-8" ?>
        <!DOCTYPE configuration  PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"  "http://ibatis.apache.org/dtd/ibatis-3-config.dtd"> <configuration>

    <typeAliases>
        <typeAlias type="mybatis.mybatisAndSpring.pojo.Person" alias="Person"/>

    </typeAliases>

    <mappers>
        <mapper resource="mybatisAndSpring/mybatisConfig/personMapper.xml" />
    </mappers>

</configuration>

3、personMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD iBatis Mapper 3.0 //EN"  "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="Liujin">
<select id="findPersonById" parameterType="int" resultType="Person">
SELECT * FROM DB2INST.TBL_P WHERE ID = #{id}
</select>
</mapper>

4、Dao实现类

package mybatis.mybatisAndSpring.dao;

import mybatis.mybatisAndSpring.pojo.Person;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;

/**  * Created by yp-tc-m-2935 on 18/4/2.  */ public class PersonDaoImpl extends SqlSessionDaoSupport implements PersonDao {
    public Person findPersonById(int id) throws Exception {
        SqlSession sqlSession = this.getSqlSession();
        Person person = (Person)sqlSession.selectOne("Liujin.findPersonById",id);
        return person;
    }
}

5、Test类

package mybatis.mybatisAndSpring;

import mybatis.mybatisAndSpring.dao.PersonDao;
import mybatis.mybatisAndSpring.pojo.Person;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**  * Created by yp-tc-m-2935 on 18/4/2.  */ public class test {
    private ApplicationContext applicationContext;
    @Before
    public void setUp(){
        applicationContext = new ClassPathXmlApplicationContext("classpath:mybatisAndSpring/springConfig/beans.xml");



    }

    @Test
    public void testRun(){
        PersonDao personDao = (PersonDao) applicationContext.getBean("personDao");
        Person person = null;
        try {
            person = personDao.findPersonById(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(person);
    }
}