众筹网项目实战

编程入门 行业动态 更新时间:2024-10-22 10:40:35

众筹网项目<a href=https://www.elefans.com/category/jswz/34/1769775.html style=实战"/>

众筹网项目实战

环境搭建

创建工程

后台项目架构图

工程的创建

		atcrowdfunding01-admin-parent groupId:com.atguigu.crowd artifactId:atcrowdfunding01-admin-parentpackaging: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.crowdartifactId: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=".0.0"xmlns:xsi=""xsi:schemaLocation=".0.0 .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><mavenpiler.source>8</mavenpiler.source><mavenpiler.target>8</mavenpiler.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=".0.0"xmlns:xsi=""xsi:schemaLocation=".0.0 .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><mavenpiler.source>8</mavenpiler.source><mavenpiler.target>8</mavenpiler.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 generatorConfigurationPUBLIC "-//mybatis//DTD MyBatis Generator Configuration 1.0//EN"".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=".0.0"xmlns:xsi=""xsi:schemaLocation=".0.0 .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 依赖 --><!-- .springframework/spring-orm --><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>${atguigu.spring.version}</version></dependency><!-- .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><!-- .aspectj/aspectjweaver --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.2</version></dependency><!--  --><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><!-- .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>

依赖信息的来源

如何获取各个依赖的信息可以到专门的网站上搜索
/
上搜索
然后去调试,根据实际运行情况,确认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=".0.0"xmlns:xsi=""xsi:schemaLocation=".0.0 .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 依赖 --><!-- .springframework/spring-orm --><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId></dependency><!-- .springframework/spring-webmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId></dependency><!-- .aspectj/aspectjweaver --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId></dependency><!--  --><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> <!-- .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=""xmlns:xsi=""xmlns:context=""xsi:schemaLocation=" .xsd  .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 {@Autowiredprivate DataSource dataSource;@Testpublic 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 configurationPUBLIC "-//mybatis//DTD Config 3.0//EN"".dtd">
<configuration></configuration>

配置spring具体配置:SqlSessionFactoryBean和MapperScannerConfigurer

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""xmlns:xsi=""xmlns:context=""xsi:schemaLocation=" .xsd  .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操作数据库

  @Autowiredprivate AdminMapper adminMapper;@Testpublic void testAdminMapperAutowired(){Admin admin = new Admin(null, "tom", "123123", "汤姆", "tom@qq", 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 如果不删除,那么 执行到这里必然会打印;如果使用日志方式打印,可以通过日志级别控制信息是否 打印。

    @Testpublic 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 依赖 --><!-- .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>

此时的日志信息

更多推荐

众筹网项目实战

本文发布于:2024-03-08 18:52:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1721906.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实战   项目   众筹网

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!