文章目录
基本的框架主要有:mybatiis(持久层/mysql)、spring、springMVC、SSM(搭建一个web工程的项目基础的框架)
一、 JDBC编码分析
存在的问题
- 每次都要加载连接,驱动名称存在一个硬编码;
- 每次都要获取连接,连接信息也存在硬编码;
- SQL和Java代码存在耦合;
- 结果集的数据类型需要判断,返回数据库的列名需要进行手动判断;
- 每次都要打开/关闭资源(频繁地打开、关闭,影响系统的性能)。
解决方案
- 在JDBC编程操作数据库前进行连接,操作后需要关闭子连接,频繁的连接释放在大的并发量影响系统性能。-> 连接池(连接复用)
- SQL语句硬编码在Java代码中,需求改变进而会改变Java代码本身。
-> 将SQL和java代码分离,将SQL语句放在配置文件(xml),需求改变只需要修改配置文件即可。 - 返回结果集存在硬编码
-> 将数据库中的数据映射成Java对象。
二、MyBatis
介绍
MyBatis 是一款优秀的持久层框架,具有以下特征:
- 支持自定义 SQL(非自定义SQL:hebineate)、存储过程以及高级映射。
- MyBatis 解决JDBC 代码 以及 设置参数 和 获取结果集的工作。
- MyBatis通过XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
原理
配置mybatis
1. 引入依赖
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
2. 全局配置文件(mybatis-config.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>
<!--配置数据源-->
<environments default="development">
<!--id:环境的唯一标识-->
<environment id="development">
<!--事务管理器-->
<transactionManager type="JDBC"></transactionManager>
<!--数据源类型-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/school"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
</configuration>
3.Pojo类
//和数据库中 Student表对应
public class Student {
private Integer SID;
private String Sname;
private Integer Sage;
private String Ssex;
@Override
public String toString() {
return "Student{" +
"SID=" + SID +
", Sname='" + Sname + '\'' +
", Sage=" + Sage +
", Ssex='" + Ssex + '\'' +
'}';
}
//还要实现getter、setter方法,此处省略
}
4.Mapper接口文件(StudentMapper.java)
public interface StudentMapper {
public Student selectStudentById(Integer SID);
}
5.配置mapper.xml文件(StudentMapper.xml)
mapper.xml配置文件中是配置SQL语句的,mapper接口文件中每一个方法在mapper.xml文件中对应一个唯一的Statement(sql)
<?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,命令空间:保证命名空间唯一,一般是对应的mapper.java的包全路径-->
<mapper namespace="mapper.StudentMapper">
<!--
selectStudentById
select * from Student where SID = XXX
-->
<!--
select标签:查询操作
id属性:statement的id,用于表示定义的SQL,在同一个命名空间中id是不允许重复的
#{XXX}:输入参数的占位符,避免SQL注入
parameterType:输入参数类型
resultType:指定结果集类型
-->
<select id="selectStudentById" parameterType="int" resultType="com.pojo.Student" >
select * from Student where SID = #{id}
</select>
</mapper>
6.将StudentMapper.xml文件引入到全局配置文件中
<!--引入mapper配置文件-->
<mappers>
<mapper resource="mapper/StudentMapper.xml"/>
<!--相对路径-->
</mappers>
使用mybatis
执行查询操作:
//查询操作
public static void select(){
//mybatis的配置文件
String resource = "mybatis-config.xml";
try {
//读取配置文件,mybatis提供了Resource类来获取配置文件流
InputStream resourceAsStream = Resources.getResourceAsStream(resource);
//创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//通过反射机制来获取mapper实例:通过动态代理机制(反射)产生了一个代理类
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.selectStudentById(10);
System.out.println(student);
} catch (IOException e) {
e.printStackTrace();
}
}
使用步骤
- 配置mybatis-config.xml全局配置文件(数据源、外部mapper)
- 创建SQLSessionFactory
- 通过SQLSessionFactory来创建SQLSession
- 通过SQLSession进行CRUD操作
- 关闭会话 SQLSession.close() 操作
其他操作
其他使用操作在MyBatis使用中介绍。
三、显示执行日志
- 引入依赖 log4j 日志框架
<!--日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
</dependency>
- 在资源路径下创建 log4j.properties
## 日志文件
## debug 级别
log4j.rootLogger=DEBUG,Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{yyyy-MM-dd-HH\:mm\:ss,SSS} [%t] [%c] [%p] - %m%n
log4j.logger.com.tulun=DEBUG /
##输出sql 语句
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG</strong>
四、其他配置
properties:读取配置属性
setting:设置
<!-- 全局配置信息-->
<settings>
<!--二级缓存的配置信息-->
<!--二级缓存/懒加载-->
<setting name="cacheEnabled" value="true"/>
</settings>
typeAliases:类型别名
<typeAliases>
<!--对于单个类取别名-->
<typeAlias type="pojo.Student" alias="student"/>
<!--批量类取别名-->
<package name="com.tulun.maventest"/>
</typeAliases>
Mappers:映射
<!--引入mapper配置文件:和Spring整合时,可以省略-->
<mappers>
<!--resource:配置单个映射文件,
mapper.java接口文件和mapper.xml文件可以不在同一路径下且名称可以不同
-->
<mapper resource="mapper/StudentMapper.xml"/><!--相对路径-->
<!--映射单个文件
指定的接口文件要遵循制定的规则:
mapper.java接口文件和mapper.xml两个文件必须放在同一路径下且名称保持相同
-->
<mapper class="mapper.StudentMapper"/>
<!--package:批量映射文件处理
遵循指定规则:与class规则相同
-->
<package name="mapper"/>
</mappers>