数据持久化
持久化就是程序的数据在持久状态和瞬时状态转换的过程
持久层
完成持久化工作的代码块,层界限十分明显
1、依赖org.mybatis mybatis 3.5.2
2、编写核心配置文件mysql mysql-connector-java 5.1.47 junit junit 4.12 test
[注]:最好不要添加中文注释
mybatis-config.xml
MybatisUtils
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
// 在使用工具时初始化
static {
try {
// 获取Mybatis获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream resourceAsStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
XXXMapper.xml
select * from mybatis.user[sql语句]
pom.xml
3、CURDsrc/main/resources ***.xml true src/main/java ***.xml true
dao创建接口方法
Mapper.xml写配置
id:方法名 parameterType:参数类型 resultType:返回类型
测试
@Test
public void Test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(接口类.class);
int result = mapper.方法名();
if (result > 0){
System.out.println("操作成功");
}
// 增删改需要提交事务
sqlSession.commit();
sqlSession.close();
}
如果实体类或者数据库中的表,字段或者参数过多,可以考虑吧使用Map
5、配置解析 1、核心配置文件优化db.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/数据库名?useSSL=true&useUnicode=true&characterEncoding=UTF-8 username=用户 password=密码
引入外部配置文件
2、类型别名(typeAliases)
方式一:给特定实体类起别名(实体类少时)
方式二:扫描实体类包,默认别名为类名,首字母小写
注解
@Alias("别名")
public class User{}
方式一:通过资源xml注册
方式二:通过类注册(不建议)
方式三:使用包扫描注册(需要取名为xxxMapper.java)
解决属性名和字段名不一致问题
7、日志 1、标准日志
2、LOG4J
导入依赖(pom.xml)
log4j log4j 1.2.17
配置(log4j.properties)
#将等级为DEBUG的日志信息输出到console和file两个目的地
log4j.rootLogger=DEBUG,console,file
#控制台输出的相关设置
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=【%c】-%m%n
#文件输出的相关配置
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/kuang.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=【%p】[%d{yy-MM-dd}【%c】%m%n
#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
启动(mybatis-config.xml)
// 创建RowBounds对象 RowBounds rowBounds = new RowBounds(1, 2); // 执行包下方法,查询出所有列表,再使用参数rowBounds参数限制大小 List3、分页插件pagehelper 9、注解开发 1.步骤userList = sqlSession.selectList("com.test.dao.UserMapper.getUserListByLimit2",null,rowBounds);
核心配置
自动提交事务
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession(true);
}
dao
// 存在多个参数必须添加@Param注解
@Select("select * from user where id=#{id}")
User getUserById(@Param("id") int id);
2.关于@Param()注解
基本类型的参数或者String类型需要加上引用类型不需要加(对象)存在多个参数必须添加@Param注解 10、Lombok
- 依赖
11、多对一(association) 1、根据查询嵌套处理(子查询)org.projectlombok lombok 1.18.12 provided
2、根据结果嵌套处理select * from mybatis.student
12、一对多(collection) 1、根据结果嵌套处理 2、根据查询嵌套处理 11&12 小结:
- 关联 - association集合 - collectionjavaType & ofType
- javaType:用来指定实体类中属性的类型ofType:用来指定映射到List或者集合中的pojo类型,类型 泛型中的约束类型
2、choose(when,otherwise)title = #{title} and author = #{author}
像switch
3、settitle=#{title} author=#{author} views=#{views}
4、trimupdate mybatis.blog where id=#{id} title=#{title}, author=#{author},
...
5、sql片段...
抽取sql标签公共部分
title = #{title} and author = #{author}
使用
注意事项:
最好基于单表来定义SQL片段不要存在where标签 6、foreach
14、缓存 1、一级缓存(sqlSession)select * from mybatis.blog id = #{id}
缓存失效:
查询不同的东西
增删改操作
查询不同的Mapper.xml
手动清理缓存
sqlSession.clearCache();
小结:一级缓存默认开启,只在一次sqlSession中有效,也就是拿到连接池到关闭连接这个区间段!一次缓存就是一个Map。
2、二级缓存开启全局缓存
在要使用二级缓存的mapper中开启
小结:
只要开启二级缓存,在同一个Mapper下就有效所有数据都会先放在一级缓存中只有当会话提交,才会提交到二级缓存中 3、ehcache自定义缓存



