栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

众筹网项目实战-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文件。



    
        atcrowdfunding01-admin-parent
        com.atguigu.crowd
        1.0-SNAPSHOT
    
    4.0.0
    war
    atcrowdfunding02-admin-webui

    
        8
        8
    

    
        
            com.atguigu.crowd
            atcrowdfunding03-admin-component
            1.0-SNAPSHOT
        
    

component的pom文件



    
        atcrowdfunding01-admin-parent
        com.atguigu.crowd
        1.0-SNAPSHOT
    
    4.0.0

    atcrowdfunding03-admin-component

    
        8
        8
    

    
        
            com.atguigu.crowd
            atcrowdfunding04-admin-entity
            1.0-SNAPSHOT
        
        
            com.atguigu.crowd
            atcrowdfunding05-common-util
            1.0-SNAPSHOT
        
    


创建数据库 理论

第一范式:数据库中的每一列都不可再分,也就是原子性

这个表中的“部门岗位”字段就可以继续拆分为“部门”字段和“岗位”字段

这样才能够专门的针对“部门”或“岗位”进行查询。
满足了第一范式。
第二范式:在满足第一范式的基础上要求每个字段都和主键完整相关,而不是仅和主键部分相关(主要针对联合主键而言)

“订单详情表”使用“订单编号”和“产品编号”联合作为主键。此时“产品价格”、“产品数量”都和联合主键整体相关,但“订单金额”和“下单时间”只是和联合主键中的“订单编号”相关,和“产品编号”无关。所以只关联了主键中的部分字段,不满足第二范式。

为了满足第二范式,需要把“订单金额”和“下单时间”移到订单表就符合第二范式了。

第三范式:满足第二范式的情况下,表中的非主键字段和主键字段直接相关,不允许间接相关

在上表中的“部门名称”和“员工编号”的关系是“员工编号”->“部门编号”->“部门名称”,不是直接相关的。此时会带来下列问题:

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文件

	
		
		
			mysql
			mysql-connector-java
			8.0.11
		
		
		
			org.mybatis
			mybatis
			3.4.6
		
		
		
			org.mybatis.generator
			mybatis-generator-core
			1.3.7
		
	

在此位置下创建文件
generatorConfig.xml





    
        
        
            
        
        
        
        

        
        
            
        

        
        

            

            
        

        
        
            
        

        
        
            

        

        
        

创建逆向工程类

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 warnings = new ArrayList();
        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依赖

     
        
            org.mybatis
            mybatis
            3.4.6
        

将实体类移到entity中com.atguigu.crowd.entity包中

为实体类admin添加无参与有参构造器与tostring方法

父工程的依赖管理

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



    4.0.0

    com.atguigu.crowd
    atcrowdfunding01-admin-parent
    1.0-SNAPSHOT
    
        atcrowdfunding02-admin-webui
        atcrowdfunding03-admin-component
        atcrowdfunding04-admin-entity
    
    pom
    
        
        4.3.20.RELEASE
        
        4.2.10.RELEASE
    

    
        
            
            
            
                org.springframework
                spring-orm
                ${atguigu.spring.version}
            
            
            
                org.springframework
                spring-webmvc
                ${atguigu.spring.version}
            
            
                org.springframework
                spring-test
                ${atguigu.spring.version}
            
            
            
                org.aspectj
                aspectjweaver
                1.9.2
            
            
            
                cglib
                cglib
                2.2
            
            
            
            
                mysql
                mysql-connector-java
                8.0.11
            
            
            
                com.alibaba
                druid
                1.0.31
            
            
            
                org.mybatis
                mybatis
                3.2.8
            
            
            
                org.mybatis
                mybatis-spring
                1.2.2
            
            
            
                com.github.pagehelper
                pagehelper
                4.0.0
            
            
            
                org.slf4j
                slf4j-api
                1.7.7
            
            
                ch.qos.logback
                logback-classic
                1.2.3
            
            
            
                org.slf4j
                jcl-over-slf4j
                1.7.25
            
            
                org.slf4j
                jul-to-slf4j
                1.7.25
            
            
            
                com.fasterxml.jackson.core
                jackson-core
                2.9.8
            
            
                com.fasterxml.jackson.core
                jackson-databind
                2.9.8
            
            
            
                jstl
                jstl
                1.2
            
            
            
                org.slf4j
                slf4j-api
                1.7.7
            
            
                ch.qos.logback
                logback-classic
                1.2.3
            
            
            
                org.slf4j
                jcl-over-slf4j
                1.7.25
            
            
                org.slf4j
                jul-to-slf4j
                1.7.25
            
            
            
                com.fasterxml.jackson.core
                jackson-core
                2.9.8
            
            
                com.fasterxml.jackson.core
                jackson-databind
                2.9.8
            
            
            
                jstl
                jstl
                1.2
            
            
            
                junit
                junit
                4.12
                test
            
            
            
                javax.servlet
                servlet-api
                2.5
                provided
            
            
            
                javax.servlet.jsp
                jsp-api
                2.1.3-b06
                provided
            
            
            
                com.google.code.gson
                gson
                2.8.5
            
            
            
                org.springframework.security
                spring-security-web
                4.2.10.RELEASE
            
            
            
                org.springframework.security
                spring-security-config
                4.2.10.RELEASE
            
            
            
                org.springframework.security
                spring-security-taglibs
                4.2.10.RELEASE
            
        
    

依赖信息的来源

如何获取各个依赖的信息可以到专门的网站上搜索
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文件里



    
        atcrowdfunding01-admin-parent
        com.atguigu.crowd
        1.0-SNAPSHOT
    
    4.0.0

    atcrowdfunding03-admin-component


    
        
            com.atguigu.crowd
            atcrowdfunding04-admin-entity
            1.0-SNAPSHOT
        
        
            com.atguigu.crowd
            atcrowdfunding05-common-util
            1.0-SNAPSHOT
        
        
        
        
            org.springframework
            spring-orm
        
        
        
            org.springframework
            spring-webmvc
        
        
        
            org.aspectj
            aspectjweaver
        
        
        
            cglib
            cglib
        
        
        
        
            mysql
            mysql-connector-java
        
        
        
            com.alibaba
            druid
        
        
        
            org.mybatis
            mybatis
        
        
        
            org.mybatis
            mybatis-spring
        
        
        
            com.github.pagehelper
            pagehelper
        
        
        
            com.fasterxml.jackson.core
            jackson-core
        
        
            com.fasterxml.jackson.core
            jackson-databind
        
        
        
            jstl
            jstl
         
        
        
            com.google.code.gson
            gson
        
    

在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容器中





    


    

        

        

        

        
    

再编写测试类对连接进行测试

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
            junit
            test
        
        
            org.springframework
            spring-test
            test
        






配置spring具体配置:SqlSessionFactoryBean和MapperScannerConfigurer





    


    

        

        

        

        
    

    

        

        

        
    


    

        
    

在测试类中测试能否通过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引入依赖

 
	org.slf4j 
	slf4j-api

 
  ch.qos.logback 
  logback-classic
 

※使用日志打印信息和使用 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里修改

      
        
        
            org.springframework
            spring-orm
            
                
                    commons-logging
                    commons-logging
                
            
        

在webui里修改

        
            org.springframework
            spring-test
            test
            
                
                    commons-logging
                    commons-logging
                
            
        

然后在component里添加转换包

 
org.slf4j 
jcl-over-slf4j

添加日志配置文件logback.xml


 
    
         
            
            [%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger] [%msg]%n
        
    
     
    
        
        
    
    
    

此时的日志信息

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/996337.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号