F:\Aziliao\mybatis\代码\31.mybatis与spring整合-开发原始dao
1.1. 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> <!-- 通过setting配置mybatis的运行参数 注意,设置运行参数会影响 mybatis的运行,一定要注意! --> <settings> <!-- 延迟加载的总开关 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 设置为false实现按需求加载 --> <setting name="aggressiveLazyLoading" value="false"/> <!-- 开启二级缓存 --> <setting name="cacheEnabled" value="true"/> </settings> <!-- 定义别名 --> <typeAliases> <!-- 单个别名定义 type:类路径 alias:别名 --> <!-- <typeAlias type="cn.itcast.mybatis.po.User" alias="user"/> --> <!-- 批量配置 指定pojo所在包路径,自动扫描包下的pojo定义别名,别名为类名(首字母小写或大写都可以) --> <package name="cn.itcast.mybatis.po"/> <!-- 如果扫描多个包中的pojo,就写多个 package--> <!-- <package name=""/> --> </typeAliases> <!-- 加载mapper.xml --> <mappers> <!-- 指定classpath下的映射文件 --> <mapper resource="sqlmap/User.xml" /> <!-- <mapper resource="mapper/UserMapper.xml" /> --> <!-- 通过mapper.java进行加载映射 class指定mapper接口 的路径 要求:mapper.java文件和mapper.xml同名且在同一个目录 --> <!-- <mapper class="cn.itcast.mybatis.mapper.UserMapper"/> --> <!-- 包扫描方式 指定mapper接口所在包,自动扫描mapper接口进行加载 要求:mapper.java文件和mapper.xml同名且在同一个目录 --> <package name="cn.itcast.mybatis.mapper"/> </mappers> </configuration>
db.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis jdbc.username=root jdbc.password=123
log4j.properties
# Global logging configuration #\u5728\u5f00\u53d1\u9636\u6bb5\uff0c\u8bbe\u7f6e\u4e3aDEBUG\uff0c\u5230\u751f\u4ea7\u73af\u5883\u4e0a\u8bbe\u7f6einfo\u6216error\u5c31\u884c\u4e86\u3002 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
1.2 在spring容器中配置sqlSessionFactory applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 配置数据源dataSource --> <!-- 加载配置文件 --> <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> <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 配置数据源 --> <property name="dataSource" ref="dataSource"/> <!-- 加载mybatis的配置文件 --> <property name="configLocation" value="classpath:SqlMapConfig.xml"/> </bean> <!-- 原始dao --> <bean id="userDao" class="cn.itcast.mybatis.dao.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> </beans>
1.3 Dao接口实现类继承SqlSessionDaoSupport
public interface UserDao { //根据用户id查询用户信息 public User findUserById(int id)throws Exception; //根据用户名称模糊查询 public List<User> findUserByName(String name)throws Exception; //添加用户 public void insertUser(User user) throws Exception; }
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { //由于继承SqlSessionDaoSupport所以这里不用再添加setSqlSessionFacotry方法了。 @Override public User findUserById(int id) throws Exception { // Sqlsession定义为局部变量 SqlSession sqlSession = this.getSqlSession(); User user = sqlSession.selectOne("test.findUserById", id); return user; } @Override public List<User> findUserByName(String name) throws Exception { // Sqlsession定义为局部变量 SqlSession sqlSession = this.getSqlSession(); List<User> list = sqlSession.selectList("test.findUserByName", "张"); return list; } @Override public void insertUser(User user) throws Exception { // Sqlsession定义为局部变量 SqlSession sqlSession = this.getSqlSession(); sqlSession.insert("test.insertUser", user); } }
1.4在spring容器中配置dao
1.15 从spring容器取出dao实例进行测试
public class UserDaoImplTest { private ApplicationContext applicationContext; @Before public void setUp() throws Exception { //取出spring的容器 applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); } @Test public void testFindUserById() throws Exception { //从容器中取出dao UserDao userDao = (UserDao) applicationContext.getBean("userDao"); //调用dao的方法 User user = userDao.findUserById(1); System.out.println(user); } }