企业级开发中框架
理论 + 实践
框架: 解决某一问题解决方案, 半成品, 使用, 快速完成项目开发
重在使用, 高度封装
SSM框架: 现在主流 Spring SpringMVC Mybatis 轻量级框架
SSH框架: SSM框架之前主流的: Spring + Struts2 + Hibernate 轻量级框架 不依赖环境
EJB框架: 重量级框架: SSH框架之前主流(SUN) 依赖开发环境, 开发环境不一样, 代码也不一样
Mybatis框架
解决dao层问题解决方案, 之前dao使用的技术: JDBC
jdbc使用步骤:
加载驱动类
创建Connection对象
编写sql语句
创建PreparedStatement对象
如果sql有?占位, 给?赋值
执行sql
如果查询, 解析ResultSet
关闭资源
问题:
-
Sql语句硬编码在代码中, 如果修改sql, 重新编译
-
JDBC步骤固定, 如果执行多次sql, 重写编写相同的代码,
-
数据源参数硬编码
MyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动、创建connection、创建statement、手动设置参数、结果集检索等jdbc繁杂的过程代码。
mybatis的难点: SQL
Mybatis执行流程图:
mybatis入门程序
-
添加依赖: mybatis, 数据库驱动(mysql), junit测试
Mysql驱动:
-
5版本: 驱动类: com.mysql.jdbc.Driver
-
8版本: 驱动类: com.mysql.cj.jdbc.Driver, 指定时区
mysql安装 5.7 开源免费的版本
Oracle收购mysql: 从8开始, 有oracle相关的功能, 社区版(免费的), 专业版(收费)
-
<!--添加依赖--> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
-
在项目下创建Mybatis全局配置文件: mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> </configuration>
-
在该配置文件创建一个数据源:
<!--开发环境--> <environments default="mysql"> <environment id="mysql"> <!--事务管理器: JDBC: 使用jdbc管理事务, 需要手动提交事务--> <transactionManager type="JDBC"/> <!--dataSource数据源的类型: POOLED 连接池 UNPOOLED: 不带连接池 --> <dataSource type="POOLED"> <!--数据库四大参数--> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="123"/> </dataSource> </environment> </environments>
-
创建表对应的实体类
使用lombok插件: 自动生成get/set 构造方法, toString hashCode …
lombok插件使用:
-
在idea安装lombok插件
-
重启idea
-
在项目中导入lombok依赖
<!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.30</version> </dependency>
-
在实体类添加lombok插件提供注解
-
package com.fs.entity; import lombok.Data; import java.util.Date; /** * @author suke * @version 1.0 * @title User * @description 用户实体类 * @create 2024/4/15 10:30 */ @Data public class User { private Integer id; private String username; private String password; private String sex; //日期类型: java.util.Date private Date brithday; private String address; }
-
编写sql映射文件 实体类名Dao.xml 或者 实体类名Mapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace: 命名空间, 随便写,后期有要求 --> <mapper namespace="user"> <!--根据id查询用户sql: 专业术语: statement 查询标签: select 属性: id: 唯一标识符, statemen的名字 parameterType: 输入参数类型, 如果是简单数据类型(八大基本数据类型, String, 日期类型) 可以省略, 如果是自定义的类类型: 一定要写 包.类名 resultType: 输出参数类型, 把结果集映射成那个java类型: 简单数据类型, 自定义的类类型 如果是多条记录, 写的集合的元素类型 sql语句的占位符: 不是?(jdbc) mybatis占位符: #{字符串} ${字符串} 如果parameterType是简单数据类型, 字符串随便写 如果parameterType是自定义的类类型: 字符串写的是类的属性名 --> <select id="selectById" parameterType="int" resultType="com.fs.entity.User"> select * from tb_user where id = #{id} </select> </mapper>
-
在mybatis的全局配置文件中,加载sql映射文件
<!--加载sql映射文件--> <mappers> <!--相对路径 判断路径是否写错, 按住ctrl不放, --> <mapper resource="UserDao.xml"/> </mappers>
-
编写代码实现查询 后期都不写 测试代码
-
加载全局配置文件
-
创建一个SqlSessionFactoryBuilder对象: 使用构建者设计模式
-
通过SqlSessionFactoryBuilder对象构建SqlSessionFactory: 工厂设计模式
-
通过SqlSessionFactory创建SqlSession对象
-
使用SqlSession对象的CURD方法操作数据
-
如果是增删改, 需要提交事务
-
关闭SqlSession
@Test public void testSelectById() throws Exception{ /* 1. 加载全局配置文件 2. 创建一个SqlSessionFactoryBuilder对象: 使用构建者设计模式 3. 通过SqlSessionFactoryBuilder对象构建SqlSessionFactory: 工厂设计模式 4. 通过SqlSessionFactory创建SqlSession对象 5. 使用SqlSession对象的CURD方法操作数据 6. 如果是增删改, 需要提交事务 7. 关闭SqlSession */ InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(in); SqlSession sqlSession = sqlSessionFactory.openSession(); //statement写法: namespace.id User user = sqlSession.selectOne("user.selectById",1); System.out.println(user); //提交事务 sqlSession.commit(); sqlSession.close(); }
在控制台打印sql语句, mybatis的执行的日志, 方便开发调式
需要使用日志框架: log4j
-
添加log4j的依赖
<!--log4j--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.36</version> </dependency>
-
添加log4j的配置文件 log4j.properties
#在开发阶段使用debug,在生产环境设置为info或者error # 日志输出级别: 从低到高: debug < info < warn(警告) < error(错误) # 设置的级别debug, 输出级别比它都会输出 log4j.rootLogger=debug,console #控制台输出 ConsoleAppender: 输出目的地: 控制台 log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n
-