2、第二节 搭建环境:持久化层
1、数据库文件
database.sql
sql
create database db_imperial_court;
use db_imperial_court;
create table t_emp
(
emp_id int primary key auto_increment,
emp_name char(100) not null,
emp_position char(100) not null,
login_account char(100) not null unique,
login_password char(100) not null
);
insert into t_emp(emp_name, emp_position, login_account, login_password)
values ('爱新觉罗·玄烨', 'emperor', 'xiaoxuanzi1654', '25325C896624D444B2E241807DCAC98B'), # 16540504
('纳兰明珠', 'minister', 'brightball1635', 'A580D0EF93C22036C859E194C14CB777'), # 16351119
('赫舍里·索额图', 'minister', 'tutu1636', 'E40FD7D49B8B7EF46F47407D583C3538'); # 17030921
create table t_memorials
(
memorials_id int primary key auto_increment,
memorials_title char(100) not null,
memorials_content varchar(5000) not null,
memorials_emp int not null,
memorials_create_time char(100),
feedback_time char(100),
feedback_content varchar(1000),
memorials_status int not null
);
insert into t_memorials(memorials_title,
memorials_content,
memorials_emp,
memorials_create_time,
feedback_time,
feedback_content,
memorials_status)
values ('浙江巡抚奏钱塘堤决口疏', '皇上啊,不好啦!钱塘江发大水啦!堤坝冲毁啦!您看这咋弄啊!', 2, '1690-05-07', null, null, 0),
('左都御史参鳌拜圈地疏', '皇上啊,鳌拜这厮不是东西呀!占老百姓的地哇!还打人呀!您看咋弄啊!', 3, '1690-04-14', null, null, 0),
('都察院劾吴三桂不臣疏', '皇上啊,不得了啦!吴三桂那孙子想造反呀!', 2, '1693-11-18', null, null, 0),
('兵部奏准噶尔犯境疏', '皇上啊,不得了啦!葛尔丹要打过来了呀!', 3, '1693-11-18', null, null, 0),
('朝鲜使臣朝拜事宜呈皇上御览', '皇上啊!朝鲜国的人要来啦!咱们请他们吃猪肉炖粉条子吧!', 2, '1680-06-11', null, null, 0),
('英吉利炮舰购买事宜疏', '皇上啊!英国的小船船咱们买多少啊?', 3, '1680-06-12', null, null, 0),
('劾杭州织造贪墨疏', '皇上啊!杭州织造有问题啊!', 2, '1680-06-13', null, null, 0),
('禀畅春园落成疏', '皇上啊!畅春园修好了哇!您啥时候过来看看呀!', 3, '1680-06-14', null, null, 0),
('请旨木兰秋狝疏', '皇上啊!秋天到啦,又该打猎啦!', 2, '1680-06-15', null, null, 0),
('核准西北军饷银两疏', '皇上啊!您看看这钱数算的对不对呀!', 3, '1680-06-16', null, null, 0),
('请旨裁撤三藩疏', '皇上啊!咱们不裁撤三藩就芭比Q了哇!', 2, '1680-06-17', null, null, 0),
('蒙古王公进京朝拜疏', '皇上啊!蒙古王公要来啦!咱们请他们吃猪肉炖粉条子吧!', 3, '1680-06-18', null, null, 0),
('礼部请旨九阿哥赐名疏', '皇上啊!您看九阿哥该叫什么名字呀?', 2, '1680-06-19', null, null, 0),
('户部尚书请旨告老还乡疏', '皇上啊!臣想回家养老啦!您看看啥时候给臣把俸禄结一下啊!', 3, '1680-06-20', null, null, 0),
('查江宁织造贪墨疏', '皇上啊!江宁织造有问题啊!', 2, '1680-06-21', null, null, 0)
;
2、Mybatis 逆向工程
demo-module06-generate/src/main/resources/generatorConfig.xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--
targetRuntime: 执行生成的逆向工程的版本
MyBatis3Simple: 生成基本的CRUD(清新简洁版)
MyBatis3: 生成带条件的CRUD(奢华尊享版)
-->
<context id="default" targetRuntime="MyBatis3">
<!-- 数据库的连接信息 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/db_imperial_court"
userId="root"
password="123456">
</jdbcConnection>
<!-- javaBean的生成策略-->
<javaModelGenerator targetPackage="com.atguigu.imperial.court.entity" targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- SQL映射文件的生成策略 -->
<sqlMapGenerator targetPackage="com.atguigu.imperial.court.mapper" targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- Mapper接口的生成策略 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.imperial.court.mapper" targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 逆向分析的表 -->
<!-- tableName设置为*号,可以对应所有表,此时不写domainObjectName -->
<!-- domainObjectName属性指定生成出来的实体类的类名 -->
<table tableName="t_emp" domainObjectName="Emp"/>
<table tableName="t_memorials" domainObjectName="Memorials"/>
</context>
</generatorConfiguration>
生成代码
bash
# 执行代码生成
$ mvn mybatis-generator:generate
自动根据表结构生成实体类和Mapper类
实体类可自行添加无参构造器、toString() 方法。
移动到对应的文件夹内
- 实体类:demo-module03-entity/src/main/java/com/atguigu/imperial/court/entity
- Mapper类:demo-module02-component/src/main/java/com/atguigu/imperial/court/mapper
- xml文件:demo-module02-component/src/main/java/com/atguigu/imperial/court/mapper/xml
配置 demo-module02-component 模块的 pom.xml
xml
<build>
<resources>
<!--编译src/main/java目录下的xml文件-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
3、建立数据库连接
数据库配置
demo-module01-web/src/main/resources/jdbc.properties
bash
dev.driverClassName=com.mysql.cj.jdbc.Driver
dev.url=jdbc:mysql://127.0.0.1:3306/db_imperial_court
dev.username=root
dev.password=123456
dev.initialSize=10
dev.maxActive=20
dev.maxWait=10000
配置数据源
demo-module01-web/src/main/resources/spring-persist.xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:mybatis="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<!-- 加载外部属性配置文件(jdbc.properties)-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据源-->
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${dev.username}"/>
<property name="password" value="${dev.password}"/>
<property name="url" value="${dev.url}"/>
<property name="driverClassName" value="${dev.driverClassName}"/>
<property name="initialSize" value="${dev.initialSize}"/>
<property name="maxActive" value="${dev.maxActive}"/>
<property name="maxWait" value="${dev.maxWait}"/>
</bean>
</beans>
数据源测试
java
package com.atguigu.imperial.court.test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(value = {"classpath:spring-persist.xml"})
public class DataSourceTest {
@Autowired
private DataSource dataSource;
private Logger logger = LoggerFactory.getLogger(DataSourceTest.class);
@Test
public void testConnection() throws SQLException {
Connection connection = dataSource.getConnection();
logger.debug(connection.toString());
// 09:55:12.544 [main] DEBUG com.atguigu.imperial.court.test.DataSourceTest - com.mysql.cj.jdbc.ConnectionImpl@770d0ea6
}
}
4、Spring 整合 Mybatis
demo-module01-web/src/main/resources/spring-persist.xml
xml
<!-- 配置 SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 装配数据源 -->
<property name="dataSource" ref="druidDataSource"/>
<!-- 指定 Mapper 配置文件的位置 -->
<property name="mapperLocations" value="classpath:com/atguigu/imperial/court/mapper/xml/*Mapper.xml"/>
</bean>
<!--https://blog.csdn.net/qq_43842093/article/details/129019078-->
<mybatis-spring:scan base-package="com.atguigu.imperial.court.mapper"/>
Mapper测试
java
package com.atguigu.imperial.court.test;
import com.atguigu.imperial.court.entity.Emp;
import com.atguigu.imperial.court.entity.EmpExample;
import com.atguigu.imperial.court.mapper.EmpMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(value = {"classpath:spring-persist.xml"})
public class EmpMappderTest {
@Autowired
private DataSource dataSource;
private Logger logger = LoggerFactory.getLogger(EmpMappderTest.class);
@Test
public void testConnection() throws SQLException {
Connection connection = dataSource.getConnection();
logger.debug(connection.toString());
}
@Autowired
private EmpMapper empMapper;
@Test
public void testEmpMapper() {
List<Emp> empList = empMapper.selectByExample(new EmpExample());
for (Emp emp : empList) {
System.out.println("emp = " + emp);
}
}
}