Mybatis spring 原始dao整合

1、 user.java

    此处略去

    db.properties

    jdbc.driver=com.mysql.jdbc.Driver

    jdbc.url=jdbc:mysql://localhost:3306/mybaits?useUnicode=true&characterEncoding=utf-8

    jdbc.username=root

    jdbc.password=*****

2、application.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:tx="http://www.springframework.org/schema/tx" 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-2.5.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	<!-- 加载数据源,使用dbcp -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
	destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
		<property name="url" value="${jdbc.url}"/>
		<property name="maxActive" value="15"/>
		<property name="maxIdle" value="5"/>
	</bean>

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation" value="SqlMapConfig.xml"></property>
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<bean id="userdao" class="com.mc.test.UserDAOImpl">
	<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
	
</beans>

3、SqlMapConfig.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>  
	<!-- 与spring整合后environments配置将删除 -->
	
	<!-- 加载属性文件 
	<properties resource="db.properties"></properties>-->
	
	<!-- setting 设置全局参数-->
	<!-- <setting></setting> -->
	<typeAliases>
		<!-- 指定单个的别名 
		<typeAlias type="com.mc.po.User" alias="user"></typeAlias>-->
		
		
		
		<!-- 批量别名定义
			指定包名,Mybatis自动扫描包中po类,自定义别名,别名就是类名(首字母大小写都可以)
		 -->
		<package name="com.mc.po"/>
	</typeAliases>
	
	
    <!-- 对事务的管理和连接池的配置  
    <environments default="development">  
        <environment id="development">  
            <transactionManager type="JDBC" />  
            <dataSource type="POOLED">  
                <property name="driver" value="${jdbc.driver}" />  
                <property name="url" value="${jdbc.url}" />  
                <property name="username" value="${jdbc.username}" />  
                <property name="password" value="${jdbc.password}" />  
            </dataSource>  
        </environment>  
    </environments>  
       -->
    <!-- mapping 文件路径配置 -->  
    <mappers>  
    	<!-- 通过resource方法一次加载一个映射文件 -->
        <!--   <mapper resource="sqlmap/User.xml"/> 
        <mapper resource="mapper/UserMapper.xml"/> -->
        
    	<!-- 通过mapper接口加载映射文件
        	遵循一些规范:前提使用的是mapper代理方法。需要将mapper接口类名和mapper.xml映射文件名称保持一致,
        	且在一个目录中
         
         <mapper class="com.mc.mapper.UserMapper"/>-->
         <mapper resource="mapper/user.xml" /> 
         
       	<!--   通过mapper接口加载多个映射文件 
       		遵循一些规范:前提使用的是mapper代理方法。需要将mapper接口类名和mapper.xml映射文件名称保持一致,
        	且在一个目录中-->
       	
         <!-- <package name="com.mapper"/> -->
         
         
         
    </mappers>  
</configuration>

4、user.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="test">
	<select id="findUserById" parameterType="int" resultType="com.mc.po.User">
		select * from user where id=#{value}
	</select>
</mapper>

5、UserDAOImpl.java

package com.mc.test;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.mc.dao.UserDAO;
import com.mc.po.User;

public class UserDAOImpl extends SqlSessionDaoSupport implements UserDAO {
	
	
	@Override
	public User findUserById(int id) throws Exception {
		//继承SqlSessionSupport,通过this.getSqlSession()得到
		SqlSession sqlsession=this.getSqlSession();
		User u=sqlsession.selectOne("findUserById",id);
		return u;
		
	}

6、UserDAOImplTest.java

package com.mc.test;

import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mc.dao.UserDAO;
import com.mc.po.User;

public class UserDAOImplTest {
	private ApplicationContext applicationContext;
	@Before
	public void setUp() throws Exception {
		applicationContext=new ClassPathXmlApplicationContext("classpath:applicationConfig.xml");
	}

	@Test
	public void testFindUserById() throws Exception {
		UserDAO userdao = (UserDAO) applicationContext.getBean("userdao");
		
			User u=userdao.findUserById(12);
		System.out.println(u.getUsername());

	}

}


你可能感兴趣的:(Mybatis spring 原始dao整合)