Spring整成Mybatis注意事项:
1. 关键jar包不能少
2.可以单独整理好Mybatis框架,测试无误再集成Spring
3.集成时,参数级别的细节可以选择忽略,但思路必须清晰
代码如下:
1.jar包
2.项目目录结构
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> <!-- 设置单个别名 --> <typeAlias type="com.lf.entity.User" alias="user"/> </typeAliases> <mappers> <!-- resource加载单个映射文件 --> <mapper resource="sqlmap/UserMapper.xml"/> </mappers> </configuration>
4.applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!--数据库连接池配置,使用dbcp--> <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}"/> </bean> <!-- sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 加载mybatis的配置文件 --> <property name="configLocation" value="mybatis/SqlMapConfig.xml" /> <!-- dataSource数据源 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 注入原始dao接口 --> <bean id="userDao" class="com.lf.dao.impl.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> </beans>
5.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="userMapper"> <select id="queryUserById" parameterType="int" resultType="user"> select user_id as id,name,age from t_user where user_id=#{id} </select> </mapper>
6.log4j.properties
log4j.rootLogger=DEBUG, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
7.jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=leifei
8.Dao接口以及Dao实现
package com.lf.dao; import com.lf.entity.User; public interface UserDao { public User queryUserById(int userId) throws Exception; }
package com.lf.dao.impl; import org.apache.ibatis.session.SqlSession; import org.mybatis.spring.support.SqlSessionDaoSupport; import com.lf.dao.UserDao; import com.lf.entity.User; public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { @Override public User queryUserById(int userId) throws Exception { SqlSession session = this.getSqlSession(); //执行查询 User user = session.selectOne("userMapper.queryUserById",userId); return user; } }
9.User实体
package com.lf.entity; public class User { private int id; private String name; private int age; private String address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
10.Junit测试类
package com.lf.test; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lf.dao.UserDao; import com.lf.entity.User; public class UserDaoImplTest { private ApplicationContext applicationContext; @Before public void setUp() throws Exception { applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml"); } @Test public void test() throws Exception { UserDao userDao = (UserDao) applicationContext.getBean("userDao"); User user = userDao.queryUserById(1); System.out.println(user); } }
11.测试结果