(一)导包
(二)配置文件+Java代码
(1)applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.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="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- mybatis的工厂 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- mybatis核心配置文件的位置 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean>
<!-- dao层的实例化
<bean id="userDao" class="cn.shu.dao.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
</bean> -->
<!-- Mapper动态代理开发,Mapper类工厂 可以产生各种Mapper实例-->
<!--
SqlSeeion sqlSession = sqlSessionFactory.openSession()
UserMapper userMapper = sqlSession.getMapper(UserMapper.class)
-->
<!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
需要sqlSessionFactory才能获得sqlSession
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
有了sqlSession,还需要给接口才能使用getMapper方法获得实例
<property name="mapperInterface" value="cn.shu.Mapper.UserMapper"></property>
</bean> -->
<!--若Mapper包下文件太多,类似上面注释的配置会非常多,重复, mapper动态开发,扫描,不需要注入工厂sqlSessionFactory,会自动寻找 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 基本包 -->
<property name="basePackage" value="cn.shu.Mapper"></property>
</bean>
</beans>
(2)db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
(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>
<typeAliases>
<package name="cn.shu.pojo"/>
</typeAliases>
<mappers>
<package name="cn.shu.mapper"/>
</mappers>
</configuration>
(4)log4j.xml
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
(5)UserMapper.java
package cn.shu.Mapper;
import cn.shu.pojo.User;
public interface UserMapper {
public User selectUserById(Integer id);
}
(6)UserMapper.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="cn.shu.Mapper.UserMapper">
<select id="selectUserById" parameterType="Integer" resultType="cn.shu.pojo.User">
select * from user where id=#{v}
</select>
</mapper>
(7)User.java
省略
(3)测试代码
package cn.shu.junit;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.shu.Mapper.UserMapper;
import cn.shu.pojo.User;
public class JunitTest {
@Test
public void test1() throws Exception {
//读取配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
/*获取bean两种方法
* 1.给接口ac.getBean(UserMapper.class)
* 2.给id(配置文件中)
* ac.getBean("userMapper")*/
UserMapper mapper = (UserMapper) ac.getBean("userMapper");
User u = mapper.selectUserById(10);
System.out.println(u);
}
@Test
public void test2() throws Exception {
//读取配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
/*获取bean两种方法
* 1.给接口ac.getBean(UserMapper.class)
* 2.给id(配置文件中,此时没有配置id,扫描Mapper会产生一堆mapper)
* ac.getBean("userMapper")*/
UserMapper mapper = (UserMapper) ac.getBean(UserMapper.class);
User u = mapper.selectUserById(16);
System.out.println(u);
}
}