众筹网项目实战-2环境搭建

环境搭建

创建工程

后台项目架构图

在这里插入图片描述

工程的创建

		atcrowdfunding01-admin-parent 
			groupId:com.atguigu.crowd 
			artifactId:atcrowdfunding01-admin-parent
			packaging:pom 
		atcrowdfunding02-admin-webui 
			groupId:com.atguigu.crowd 
			artifactId:atcrowdfunding02-admin-webui 
			packaging:war 
		atcrowdfunding03-admin-component 
			groupId:com.atguigu.crowd 
			artifactId:atcrowdfunding03-admin-component 
			packaging:jar 
		atcrowdfunding04-admin-entity 
			groupId:com.atguigu.crowd
			artifactId:atcrowdfunding04-admin-entity 
			packaging:jar 
		atcrowdfunding05-common-util 
			groupId:com.atguigu.crowd 
			artifactId:atcrowdfunding05-common-util 
			packaging:jar 
		atcrowdfunding06-common-reverse 
			groupId:com.atguigu.crowd 
			artifactId:atcrowdfunding06-common-reverse 
			packaging:jar

创建完成以后的目录情况
在这里插入图片描述

在webui中要创建web.xml

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
点击ok就创建好了
在这里插入图片描述
显示这样就完成了

依赖关系

webui 依赖 component 
component 依赖 entity 
component 依赖 util

webui 中的pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>atcrowdfunding01-admin-parent</artifactId>
        <groupId>com.atguigu.crowd</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <artifactId>atcrowdfunding02-admin-webui</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.atguigu.crowd</groupId>
            <artifactId>atcrowdfunding03-admin-component</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

component的pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>atcrowdfunding01-admin-parent</artifactId>
        <groupId>com.atguigu.crowd</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>atcrowdfunding03-admin-component</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.atguigu.crowd</groupId>
            <artifactId>atcrowdfunding04-admin-entity</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.atguigu.crowd</groupId>
            <artifactId>atcrowdfunding05-common-util</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

创建数据库

理论

第一范式:数据库中的每一列都不可再分,也就是原子性
在这里插入图片描述

这个表中的“部门岗位”字段就可以继续拆分为“部门”字段和“岗位”字段
在这里插入图片描述

这样才能够专门的针对“部门”或“岗位”进行查询
满足了第一范式。
第二范式:在满足第一范式的基础上要求每个字段都和主键完整相关,而不是仅和主键部分相关(主要针对联合主键而言)
在这里插入图片描述
“订单详情表”使用“订单编号”和“产品编号”联合作为主键。此时“产品价格”、“产品数量”都和联合主键整体相关,但“订单金额”和“下单时间”只是和联合主键中的“订单编号”相关,和“产品编号”无关。所以只关联了主键中的部分字段,不满足第二范式。

为了满足第二范式,需要把“订单金额”和“下单时间”移到订单表就符合第二范式了。
在这里插入图片描述
第三范式:满足第二范式的情况下,表中的非主键字段和主键字段直接相关,不允许间接相关
在这里插入图片描述
在上表中的“部门名称”和“员工编号”的关系是“员工编号”->“部门编号”->“部门名称”,不是直接相关的。此时会带来下列问题:

1.数据冗余:“部门名称”多次重复出现。
2.插入异常:组建一个新部门时没有员工信息,也就无法单独插入部门信息。就算强行插入部门信息,员工表中没有员工信息的记录同样是非法记录。
3.删除异常:删除员工信息会连带删除部门信息导致部门信息以外丢失。
4.更新异常:哪怕只修改一个部门的名称也要更新多条员工记录。

正确做法是:把上表拆分为俩张表,以外键形式关联
在这里插入图片描述
“部门编号”和“员工编号”是直接相关的。
第二范式的另一种表述方式是:俩张表通过外键关联,不保存冗余字段。例如:不能再“员工表”中存储“部门名称”。

实践

规则的变通:
三大范式是设计数据库表结构的规则约束,但是在实际开发中允许局部变通。

比如为了快速查询到关联数据可能会允许冗余字段的存在。
例如:在员工表中存储部门名称虽然违背第三范式,但是免去了对部门表的关联查询。

根据业务功能设计数据库表

	看得见的字段
	能够从需求文档或原型页面上直接看到的数据都需要设计对应的数据库表、字段来存储。

在这里插入图片描述

根据上面的原型页面我们可以看到管理员表需要包含字段如下:
	账号
	密码
	名称
	邮箱地址

看不见的字段

除了能够直接从需求文档中看到的字段,实际开发中往往还会包含一些其他字段来保存其他相关数据。
例如:管理员表需要再添加如下字段以有利于数据维护
	主键
	创建时间
	修改时间

冗余字段

为了避免建表时考虑不周有所遗漏,到后期再修改表结构非常麻烦,
所以也有的团队会设置一些额外的冗余字段备用。

实际开发对接

实际开发中储粮一些各个模块都需要使用的公共表在项目启动时创建好,其他书友各个模块的表由改模块的负责人创建。

创建数据库

CREATE DATABASE `project_crowd` CHARACTER SET utf8;

创建管理员数据库表

use project_crowd;
 drop table if exists t_admin; 
 create table t_admin (
  id int not null auto_increment, # 主键 
 login_acct varchar(255) not null, # 登录账号 
 user_pswd char(32) not null, # 登录密码 
 user_name varchar(255) not null, # 昵称 
 email varchar(255) not null, # 邮件地址 
 create_time char(19), # 创建时间 
 primary key (id)  #设置id为主键
 );

###Mybatis逆向工程
先配置pom文件

	<dependencies>
		<!-- mysql驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.11</version>
		</dependency>
		<!-- mybatis依赖 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.6</version>
		</dependency>
		<!-- mybatis逆向工程依赖 -->
		<dependency>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-core</artifactId>
			<version>1.3.7</version>
		</dependency>
	</dependencies>

在此位置下创建文件
generatorConfig.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>
    <context id="atguiguTables" targetRuntime="MyBatis3">
        <!-- 去除生成的注解 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!-- 数据库连接配置 -->
        <!-- 注意xml中不支持&,用&amp;代替 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/project_crowd?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=GMT%2B8"
                        userId="root" password="123456"></jdbcConnection>

        <!-- 处理NUMERIC和DECIMAL类型的策略 -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 配置Entity类的生成的位置 -->
        <javaModelGenerator targetPackage="com.atguigu.crowd.entity"
                            targetProject=".\src\main\java">
<!--            enableSubPackages:是否让schema作为包的后缀-->
            <property name="enableSubPackages" value="false" />
<!--            从数据库返回的值被清理前后的空格-->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- 配置sql映射文件的生成位置 -->
        <sqlMapGenerator targetPackage="com.atguigu.crowd.mapper"
                         targetProject=".\src\main\java">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- 配置dao接口的生成位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.atguigu.crowd.mapper"
                             targetProject=".\src\main\java">
            <property name="enableSubPackages" value="false" />

        </javaClientGenerator>

        <!-- 指定逆向依据的数据表 -->
        <table tableName="t_admin" domainObjectName="Admin"></table>
    </context>
</generatorConfiguration>

创建逆向工程类
在这里插入图片描述

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class GeneratorTest {
    
    public static void main(String[] args) throws Exception {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("D:\\workspace\\atcrowdfunding06-common-reverse\\src\\main\\resources\\generatorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
        System.out.println("生成成功!");
    }

}

生成完毕以后将AdminMapper.xml移到webui中resource/mybatis/mapper中

在这里插入图片描述
将AdminMapper移到component里com.atguigu.corwd.mapper包里
在这里插入图片描述
并且需要在pom文件里添加mybatis依赖

     <!-- mybatis依赖 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>

将实体类移到entity中com.atguigu.crowd.entity包中
在这里插入图片描述
为实体类admin添加无参与有参构造器与tostring方法
在这里插入图片描述

父工程的依赖管理

在admin-parent父工程中的pom里添加依赖管理

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.atguigu.crowd</groupId>
    <artifactId>atcrowdfunding01-admin-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>atcrowdfunding02-admin-webui</module>
        <module>atcrowdfunding03-admin-component</module>
        <module>atcrowdfunding04-admin-entity</module>
    </modules>
    <packaging>pom</packaging>
    <properties>
        <!-- 声明属性,对 Spring 的版本进行统一管理 -->
        <atguigu.spring.version>4.3.20.RELEASE</atguigu.spring.version>
        <!-- 声明属性,对 SpringSecurity 的版本进行统一管理 -->
        <atguigu.spring.security.version>4.2.10.RELEASE</atguigu.spring.security.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Spring 依赖 -->
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${atguigu.spring.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${atguigu.spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${atguigu.spring.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.2</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/cglib/cglib -->
            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
                <version>2.2</version>
            </dependency>
            <!-- 数据库依赖 -->
            <!-- MySQL 驱动 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.11</version>
            </dependency>
            <!-- 数据源 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.31</version>
            </dependency>
            <!-- MyBatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.2.8</version>
            </dependency>
            <!-- MyBatis 与 Spring 整合 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.2.2</version>
            </dependency>
            <!-- MyBatis 分页插件 -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>4.0.0</version>
            </dependency>
            <!-- 日志 -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.7</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.2.3</version>
            </dependency>
            <!-- 其他日志框架的中间转换包 -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>1.7.25</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jul-to-slf4j</artifactId>
                <version>1.7.25</version>
            </dependency>
            <!-- Spring 进行 JSON 数据转换依赖 -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.9.8</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.9.8</version>
            </dependency>
            <!-- JSTL 标签库 -->
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <!-- 日志 -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.7</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.2.3</version>
            </dependency>
            <!-- 其他日志框架的中间转换包 -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>1.7.25</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jul-to-slf4j</artifactId>
                <version>1.7.25</version>
            </dependency>
            <!-- Spring 进行 JSON 数据转换依赖 -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.9.8</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.9.8</version>
            </dependency>
            <!-- JSTL 标签库 -->
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <!-- junit 测试 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
            <!-- 引入 Servlet 容器中相关依赖 -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
                <scope>provided</scope>
            </dependency>
            <!-- JSP 页面使用的依赖 -->
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.1.3-b06</version>
                <scope>provided</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>2.8.5</version>
            </dependency>
            <!-- SpringSecurity 对 Web 应用进行权限管理 -->
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-web</artifactId>
                <version>4.2.10.RELEASE</version>
            </dependency>
            <!-- SpringSecurity 配置 -->
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-config</artifactId>
                <version>4.2.10.RELEASE</version>
            </dependency>
            <!-- SpringSecurity 标签库 -->
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-taglibs</artifactId>
                <version>4.2.10.RELEASE</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

依赖信息的来源

如何获取各个依赖的信息可以到专门的网站上搜索
https://mvnrepository.com/
上搜索
然后去调试,根据实际运行情况,确认jar包之间是否兼容
例如:SpringMVC需要jackson的支持,来处理JSON数据,但是SpringMVC并没有依赖jackson。所以需要我们之间保证jar包之间的兼容性。

一般公司都有自己的parent父项目,直接管理你所需的框架以及版本,都是爬过很多坑以及无数次调试得到的总结一般不会轻易修改。

Spring整合Mybatis

目标:adminMapper通过ioc容器装配到当前组件中后,就可以直接调用它的方法,享用到框架给我们提供的方便。
例如:下面代码
在这里插入图片描述
思路
mybatis根据AdminMapper.xml里的sql动态创建类,然后使用这个类创建我们所使用的对象
使用MapperScannerConfigurer扫描器去扫描所在的包,把对象扫描到ioc容器中
使用spring的扫描是扫描不到mybatis的mapper的
还需要将AdminMapper.xml读取到,就要配置SqlSessionFactoryBean里的属性MapperLocations
SqlSessionFactoryBean还需要配置数据源信息DataSource。
在这里插入图片描述
上图就是整个流程,我们只需实现虚线以外的操作即可,虚线里的都是框架帮我们完成的。

我们需要将service加入到ioc容器
从ioc容器中找到对应类型的bean进行装配
使用MapperScannerConfigurer扫描mapper所在的包
编写jdbc.properties
编写SqlSessionFactoryBean信息:装配数据源DataSource,指定mapper文件位置,指定mybatis-config.xml文件

整合步骤

首先在子工程中加入搭建环境所需要的具体依赖
准备jdbc.properties
创建spring配置文件专门配置spring和mybatis整合相关
在spring的配置文件中加载jdbc.properties属性文件
配置数据源
测试从数据源中能不能获取数据库连接
配置SqlSessionFactoryBean
	装配数据源
	指定Xxxmapper.xml配置文件的位置
	指定Mybatis全局配置文件的位置(如果需要就配置)
配置MapperScannerConfigurer
测试是否可用装配Xxxmapper接口是否可以通过这个接口操作数据库

在子工程component中的pom文件里

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>atcrowdfunding01-admin-parent</artifactId>
        <groupId>com.atguigu.crowd</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>atcrowdfunding03-admin-component</artifactId>


    <dependencies>
        <dependency>
            <groupId>com.atguigu.crowd</groupId>
            <artifactId>atcrowdfunding04-admin-entity</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.atguigu.crowd</groupId>
            <artifactId>atcrowdfunding05-common-util</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- Spring 依赖 -->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/cglib/cglib -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
        </dependency>
        <!-- 数据库依赖 -->
        <!-- MySQL 驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- 数据源 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <!-- MyBatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <!-- MyBatis 与 Spring 整合 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </dependency>
        <!-- MyBatis 分页插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
        </dependency>
        <!-- Spring 进行 JSON 数据转换依赖 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <!-- JSTL 标签库 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
        </dependency> 
        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
    </dependencies>
</project>

在webui中创建jdbc.properties
在这里插入图片描述

jdbc.user=root
jdbc.password=123456
jdbc.url=jdbc:mysql://localhost:3306/project_crowd?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
jdbc.driver=com.mysql.cj.jdbc.Driver

创建spring-persist-mybatis.xml配置文件
在这里插入图片描述
在spring配置文件里扫描jdbc配置信息,将信息装配到ioc容器中

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!--    加载jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

<!--    配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--        连接数据库用户名-->
        <property name="username" value="${jdbc.user}"/>
<!--        连接数据库的密码-->
        <property name="password" value="${jdbc.password}"/>
<!--        目标数据库的url地址-->
        <property name="url" value="${jdbc.url}"/>
<!--        数据库驱动全类名-->
        <property name="driverClassName" value="${jdbc.driver}"/>
    </bean>
</beans>

再编写测试类对连接进行测试
在这里插入图片描述

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

//指定spring给Junit提供的运行器类
@RunWith(SpringJUnit4ClassRunner.class)
//加载spring配置文件的注解
@ContextConfiguration(locations = {"classpath:spring-persist-mybatis.xml"})
public class CrowdSpringTest {

    @Autowired
    private DataSource dataSource;

    @Test
    public void testDataSource() throws SQLException{

        //1.通过数据源对象获取数据源连接
        Connection connection = dataSource.getConnection();
        //2.打印数据库连接
        System.out.println(connection);
    }
}

注意:为了能够在webui工程中执行Junit需要吧spring-test和junit依赖添加到webui工程中

 <!-- junit 测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>

在这里插入图片描述

<?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>

</configuration>

配置spring具体配置:SqlSessionFactoryBean和MapperScannerConfigurer

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!--    加载jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

<!--    配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--        连接数据库用户名-->
        <property name="username" value="${jdbc.user}"/>
<!--        连接数据库的密码-->
        <property name="password" value="${jdbc.password}"/>
<!--        目标数据库的url地址-->
        <property name="url" value="${jdbc.url}"/>
<!--        数据库驱动全类名-->
        <property name="driverClassName" value="${jdbc.driver}"/>
    </bean>
<!--    配置SqlSessionFactoryBean-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--        装配数据源-->
        <property name="dataSource" ref="dataSource"/>
<!--        指定mybatis全局配置文件位置-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--        指定Mapper配置文件-->
        <property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"/>
    </bean>
<!--配置MapperScannerConfigurer-->
<!--    把Mybatis创建的Mapper接口类型的代理对象扫描到ioc容器中-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--        使用basePackage属性指定Mapper接口所在包-->
        <property name="basePackage" value="com.atguigu.crowd.mapper"/>
    </bean>
</beans>

在测试类中测试能否通过mapper操作数据库

  @Autowired
    private AdminMapper adminMapper;

    @Test
    public void testAdminMapperAutowired(){
        Admin admin = new Admin(null, "tom", "123123", "汤姆", "tom@qq.com", null);
        int insert = adminMapper.insert(admin);
        System.out.println(insert);
    }

结果为:1
在这里插入图片描述
数据库结果:
在这里插入图片描述

日志管理

在这里插入图片描述
spring使用自带的commons-logging日志包,打印的效果如下。
在这里插入图片描述

我们在component的pom引入依赖

<dependency> 
	<groupId>org.slf4j</groupId> 
	<artifactId>slf4j-api</artifactId>
</dependency>
<dependency> 
  <groupId>ch.qos.logback</groupId> 
  <artifactId>logback-classic</artifactId>
 </dependency>

在这里插入图片描述

※使用日志打印信息和使用 sysout 打印信息的区别:sysout 如果不删除,那么 执行到这里必然会打印;如果使用日志方式打印,可以通过日志级别控制信息是否 打印。

    @Test
    public void testLog(){
        //1.获取Logger对象,这里传入的Class对象就是当前打印日志类
        Logger logger = LoggerFactory.getLogger(CrowdSpringTest.class);
        //2.根据不同日志级别打印日志
        logger.debug("Hello I am Debug level!!!");
        logger.debug("Hello I am Debug level!!!");
        logger.debug("Hello I am Debug level!!!");

        logger.info("Info level!!!");
        logger.info("Info level!!!");
        logger.info("Info level!!!");

    }

在这里插入图片描述
我们要更换日志就需要将pom依赖的commons-logging排除掉
在component里修改

      <!-- Spring 依赖 -->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

在webui里修改

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

然后在component里添加转换包

<dependency> 
<groupId>org.slf4j</groupId> 
<artifactId>jcl-over-slf4j</artifactId>
</dependency>

添加日志配置文件logback.xml
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true"> <!-- 指定日志输出的位置 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder> <!-- 日志输出的格式 -->
            <!-- 按照顺序分别是:时间、日志级别、线程名称、打印日志的类、日志主体 内容、换行 -->
            <pattern>[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger] [%msg]%n</pattern>
        </encoder>
    </appender>
    <!-- 设置全局日志级别。日志级别按顺序分别是:DEBUG、INFO、WARN、ERROR --> <!-- 指定任何一个日志级别都只打印当前级别和后面级别的日志。 -->
    <root level="INFO">
        <!-- 指定打印日志的 appender,这里通过“STDOUT”引用了前面配置的 appender -->
        <appender-ref ref="STDOUT"/>
    </root>
    <!-- 根据特殊需求指定局部日志级别 -->
    <logger name="com.atguigu.crowd.mapper" level="DEBUG"/>
</configuration>

此时的日志信息
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值