具体的感兴趣可以参考:MyBatis
此时此刻,没用的话不再多说了,直接开始代码工程吧!
整体的代码实现:
具体使用到的我们在进行细说
基本上理解一边就能会使用整合
准备工作:
db.properties
复制代码
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3307/shopping
jdbc.username=root
jdbc.password=12345
复制代码
log4j.properties
复制代码
#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
复制代码
applivationContext.xml
mvc,context,aop,tx,beans...命名约束
复制代码
复制代码
使用dbcp数据库连接池
复制代码
复制代码
配置SqlSessionFactory
不需要在类中单独进行配置
复制代码
复制代码
SqlMapConfig.xml
复制代码
复制代码
其他的设置项在使用的时候在进行添加,,,,,,
一.dao开发
Userser.xml
简单的小案例进行测试,其余的代码和之前的方法一样
复制代码
复制代码
UserDao.java
复制代码
public interface UserDao {
//根据id查询用户信息
public User findUserById(int id) throws Exception;
}
复制代码
UserDaoImp.java
让实现接口类的类继承SqlSessionDaoSupport
通过spring进行注入,声明配置方式,配置dao的bean
此时不需要手动配置SqlSessionFactory
直接可以得到其对象
此时不需要手动关闭,在spring容器池里会自动关闭
复制代码
public class UserDaoImp extends SqlSessionDaoSupport implements UserDao {
@Override
public User findUserById(int id) throws Exception {
SqlSession sqlSession = this.getSqlSession();
User user =sqlSession.selectOne("test.findUserByID", id);
return user;
}
}
复制代码
applivationContext.xml
注入sqlSessionFactory
复制代码
复制代码
测试:
复制代码
public class Test {
private ApplicationContext applicationContext;
public ApplicationContext getApplication(){
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
return applicationContext;
}
//Dao开发
@org.junit.Test
public void testDao() throws Exception{
UserDao userdao = (UserDao) getApplication().getBean("userDao");
User user = userdao.findUserById(1);
System.out.println(user);
}
}
复制代码
复制代码
DEBUG [main] - ==> Preparing: select * from user where id=?
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <== Total: 1
DEBUG [main] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bf7ca37]
DEBUG [main] - Returning JDBC Connection to DataSource
User [id=1, username=王五, birthday=null, sex=2, address=null]
复制代码
二.Mapper代理开发
UserMapper.java
复制代码
//相当于dao接口
public interface UserMapper {
//根据id查询用户信息
public User findUserById(int id) throws Exception;
}
复制代码
UserMapper.xml
复制代码
复制代码
注意上述的两个文件在一个路径下。
SqlMapConfig.xml
复制代码
复制代码
通过MapperFactoryBean创建代理对象
复制代码
复制代码
测试:
复制代码
public class TestMapper {
private ApplicationContext applicationContext;
public ApplicationContext getApplication(){
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
return applicationContext;
}
//Mapper开发
@org.junit.Test
public void testmapper() throws Exception{
UserMapper user = (UserMapper) getApplication().getBean("userMapper");
User u = user.findUserById(1);
System.out.println(u);
}
}
复制代码
复制代码
DEBUG [main] - ==> Preparing: SELECT * FROM USER WHERE id=?
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <== Total: 1
DEBUG [main] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7c729a55]
DEBUG [main] - Returning JDBC Connection to DataSource
User [id=1, username=王五, birthday=null, sex=2, address=null]
复制代码
如果有很多个类都需要对每个mapper进行配置,此时需要大量的工程???
复制代码
。。。。。
复制代码
通过MapperScannerConfigurer进行mapper扫描(建议使用)
applicationContext.xml
说明:
mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册
遵循规范:将mapper.java和mapper.xml映射文件名称保持一致,且在一个目录 中
自动扫描出来的mapper的bean的id为mapper类名(首字母小写)
name="basePackage"
指定扫描的包名
如果扫描多个包,每个包中间使用半角逗号分隔
复制代码
复制代码
其余的测试不变......
https://www.cnblogs.com/Mrchengs/p/9846088.html