一 环境搭建
1 maven jar包
mysql
mysql-connector-java
5.1.18
c3p0
c3p0
0.9.1.2
javax.servlet
javax.servlet-api
3.0.1
provided
javax.servlet.jsp
jsp-api
2.2
provided
jstl
jstl
1.2
taglibs
standard
1.1.2
junit
junit
4.12
net.sf.json-lib
json-lib
2.4
jdk15
org.springframework
spring-core
4.1.7.RELEASE
org.springframework
spring-beans
4.1.7.RELEASE
org.springframework
spring-context
4.1.7.RELEASE
org.springframework
spring-jdbc
4.1.7.RELEASE
org.springframework
spring-tx
4.1.7.RELEASE
org.springframework
spring-web
4.1.7.RELEASE
org.springframework
spring-webmvc
4.1.7.RELEASE
org.aspectj
aspectjweaver
1.8.6
org.springframework
spring-test
4.1.7.RELEASE
org.mybatis
mybatis
3.3.0
org.mybatis
mybatis-spring
1.2.3
cglib
cglib
2.2
asm
asm
3.3.1
org.slf4j
slf4j-api
1.7.5
org.slf4j
slf4j-log4j12
1.7.12
log4j
log4j
1.2.17
2 db.properties
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url =jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
jdbc.username =root
jdbc.password = root
3 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=%5p [%t] - %m%n
二 原始dao方法
1 定义接口(User.java)
package edu.eurasia.dao;
import edu.eurasia.entity.UserBean;
public interface UserDao {
UserBean findUser(int id) throws Exception;
}
2 接口实现(UserImpl.java)
- 继承SqlSessionDaoSupport,使用getSqlSession获取sqlSession,不用手动注入sqlSessionFactory
package edu.eurasia.dao.impl;
import edu.eurasia.dao.User;
import edu.eurasia.entity.UserBean;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
public class UserImpl extends SqlSessionDaoSupport implements User {
public UserBean findUserById(int id) throws Exception {
SqlSession sqlSession = this.getSqlSession();
UserBean userBean = sqlSession.selectOne("test.findUserById",id);
return userBean;
}
}
3 User.xml文件配置(User.xml)
4 配置文件引入mybatis配置文件(mybatis-config.xml)
5 配置spring核心配置文件(applicationContext.xml)
6 测试(UserText.java)
package edu.eurasia;
import edu.eurasia.dao.User;
import edu.eurasia.entity.UserBean;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserText {
private ApplicationContext applicationContext;
@Before
public void setUp()throws Exception{
//获取spring核心配置文件
applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
@Test
public void findUserById() throws Exception{
//根据id获取注入sqlSessionFactory的方法
User user = (User) applicationContext.getBean("userDao");
UserBean userBean = user.findUserById(1);
System.out.println(userBean);
}
}
三 mapper自动映射方法
1 方法一
(1) 定义mapper sql核心配置文件(UserMapper.xml)
((2) 定义接口(UserMapper.java)
package edu.eurasia.dao;
import edu.eurasia.entity.UserBean;
public interface UserMapper {
UserBean findUserByIdMapper(int id) throws Exception;
}
((3) 配置文件引入mybatis配置文件(mybatis-config.xml)
((4) spring核心配置文件(applicationContext.xml)
((5) 测试(MapperText.java)
package edu.eurasia;
import edu.eurasia.dao.UserMapper;
import edu.eurasia.entity.UserBean;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MapperText {
private ApplicationContext applicationContext;
@Before
public void setUp()throws Exception{
applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
@Test
public void findUserById() throws Exception{
UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
UserBean userBean = userMapper.findUserByIdMapper(1);
System.out.println(userBean);
}
}
缺点
- MapperFactoryBean 需要针对每个mapper进行配置
2 方法 二
- 针对方法一存在问题,做以下改进
(1) 优化applicationContext.xml配置文件
- mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册
- 遵循规范:将mapper.java和mapper.xml映射文件名称保持一致,且在一个目录 中
- 自动扫描出来的mapper的bean的id为mapper类名(首字母小写)
- 如果扫描多个包,每个包中间使用半角逗号分隔
(2) 测试
package edu.eurasia;
import edu.eurasia.dao.UserMapper;
import edu.eurasia.entity.UserBean;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MapperText {
private ApplicationContext applicationContext;
@Before
public void setUp()throws Exception{
applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
@Test
public void findUserById() throws Exception{
UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
UserBean userBean = userMapper.findUserByIdMapper(3);
System.out.println(userBean);
}
}
总结
- spring整合mabatis,使用方法二也就是自动扫描时,理论是不需要在mybatis核心配置文件中引入sql配置文件的,可事实是不引入会报错(不明,待解决)
问题解决2018-4-25
阐述
在eclipse中mapper文件和mapper.xml配置文件是在同一目录下,所以上述配置就可以实现mapper的动态代理。
但是,在idea中,所有的配置文件都是在resources资源文件下,就引起mapper文件和mapper.xml配置文件不在同一目录下,所以,就会出现上述错误。
解决
在spring-dao配置文件中,在配置SqlSessionFactory对象时,加入自动扫描mapper.xml的配置文件