尚筹网(一)Idea下的环境搭建

一、创建数据库、管理员数据表

#创建数据库
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, #ID(主键)
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) #设置主键
);

二、创建工程

1)在Idea中,File - New - Project - EmptyProject,创建一个空项目

2)创建父工程(Module)atcrowdfunding01-admin-parent
groupId:com.atguigu.crowd
artifactId:atcrowdfunding01-admin-parent
packaging:pom
*可以删除父工程的src目录

3)创建子工程
atcrowdfunding02-admin-webui、
atcrowdfunding03-admin-component、
atcrowdfunding04-admin-entity

groupId:com.atguigu.crowd
artifactId:atcrowdfunding02-admin-webui
packaging:war

groupId:com.atguigu.crowd
artifactId:atcrowdfunding03-admin-component
packaging:jar

groupId:com.atguigu.crowd
artifactId:atcrowdfunding04-admin-entity
packaging:jar

4)创建同级工程
atcrowdfunding05-common-util
atcrowdfunding06-common-reverse

groupId:com.atguigu.crowd
artifactId:atcrowdfunding05-common-util
packaging:jar

groupId:com.atguigu.crowd
artifactId:atcrowdfunding06-common-reverse
packaging:jar

5)建立工程之间的关系

webui 依赖 component
component 依赖 entity
component 依赖 util
在这里插入图片描述

6)添加打包类型
webui:

<packaging>war</packaging>

7)尝试对项目进行打包

报错:The POM for com.atguigu.crowd:atcrowdfunding05-common-util:jar:1.0-SNAPSHOT is missing, no dependency information available
原因:在component模块中引用了util模块,但是在本地库中找不到util的资源。
解决方案:先对util模块进行maven-install操作,将其安装到maven本地仓库

再次打包

报错:Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project atcrowdfunding02-admin-webui: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)

原因:webui打的是war包,缺少web.xml文件无法打包
解决方案:在src/main目录下创建webapp/WEB-INF文件夹,并创建web.xml文件

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
</web-app>

三、Mybatis逆向工程

1)reverse工程的pom配置

<dependencies>
<!--Mybatis核心-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
</dependencies>

<build>
<!--构建过程中用到的插件-->
<plugins>

<!--具体插件,逆向工程的操作时以构架过程中插件形式出现的-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>

<!--插件的依赖-->
<dependencies>

<!--逆向工程的核心依赖-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>

<!--数据库连接池-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>

<!--Mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>

</dependencies>
</plugin>
</plugins>
</build>

2)reverse工程的resources目录下的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="test" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>

<!-- 数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/project_crowd?characterEncoding=utf8"
userId="root"
password="666666">
</jdbcConnection>

<!-- 类型转换 -->
<javaTypeResolver>
<!--默认为false,会把DECIMAL和NUMERIC类型解析为Integer,
为true时把DECIMAL和NUMERIC解析为java.math.BigDecimal -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>

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

<!-- 生成mapper映射文件的包名和位置-->
<sqlMapGenerator targetPackage="com.atguigu.crowd.mapper" targetProject=".\src\main\java">
<!-- enableSubPackages:是否让 schema 作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>

<!-- 生成mapper类的包名和位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.crowd.mapper" targetProject=".\src\main\java">
<!-- enableSubPackages:是否让 schema 作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>

<!-- 数据库表名字和我们的 entity 类对应的映射指定 -->
<table tableName="t_admin" domainObjectName="Admin" />

</context>
</generatorConfiguration>

3)在atcrowdfunding06-common-reverse点击界面右边的
Maven—>Plugins—>mybatis-generator下的mybatis-generator:generate命令,代码生成,
并在Admin.java文件添加无参构造、有参构造、tostring方法

4)逆向工程生成的资源各归各位
WebUI 工程将来在 Tomcat 上运行时,现在 resources 目录下的资源会直接放在 WEB-INF/classes 目录(也就是类路径)下,所以放在 resources 目录下运行的时候更容 易找到
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

报错:Mapper接口的@Param注解报红,因为没有添加Mybats依赖

解决方案:在atcrowdfunding03-admin-component的pom下添加依赖包

<!-- 依赖 MyBatis 核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>

四、父工程依赖管理

1)版本声明

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

2)依赖管理

<dependencyManagement>

<dependencies>

<!-- Spring 依赖 -->

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${atguigu.spring.version}</version>
</dependency>

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

<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>5.1.3</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>

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

<!--自用JSON转换工具包-->
<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>${atguigu.spring.security.version}</version>
</dependency>
<!-- SpringSecurity 配置 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${atguigu.spring.security.version}</version>
</dependency>
<!-- SpringSecurity 标签库 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${atguigu.spring.security.version}</version>
</dependency>

</dependencies>
</dependencyManagement>

五、Spring整合Mybatis

1)在子工程component加入搭建环境所需依赖

<!-- Spring 核心 -->

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>

<!--AOP依赖-->

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

<!--自用JSON转换工具包-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

2)准备jdbc.properties(在webui/src/main/resources)

jdbc.user=root
jdbc.password=666666
jdbc.url=jdbc:mysql://localhost:3306/project_crowd?useUnicode=true&characterEncoding=UTF-8&useSSL=false
jdbc.driver=com.mysql.jdbc.Driver

3)准备mybatis-config.xml(在webui/src/main/resources/mybatis)

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

4)创建 spring-persist-mybatis.xml(在webui/src/main/resources)
具体配置:
1、配置数据源

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

<!--加载外部属性文件-->
<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}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
</bean>

</beans>

2、测试数据(在webtui)
a)因为测试需要依赖,所以在webui中加入测试相关的依赖,并赋予scope为test

<!-- junit 测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

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

b)编写测试类:在webui的test/com.atguigu.corwd.test创建CrowdTest

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-persist-mybatis.xml")
public class CrowdTest {

//测试数据源
@Autowired
private DataSource dataSource;

@Test
public void testDataSource() throws SQLException {
Connection connection = dataSource.getConnection();
System.out.println(connection);
}

}

测试结果:com.mysql.jdbc.JDBC4Connection@56620197

3、配置SqlSessionFactoryBean
作用:Mybatis的核心对象

<!--配置SqlSessionFactoryBean整合MyBatis-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<!--指定Mybatis全局配置文件位置-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>

<!--指定Mapper.xml配置文件位置-->
<property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"/>

<!--装配数据源-->
<!--ref和value的区别:
ref:关联其它bean
value:设置值
-->
<property name="dataSource" ref="dataSource"/>
</bean>

4、配置MapperScannerConfigurer来扫描Mapper接口所在的包

<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.atguigu.crowd.mapper"/>
</bean>

5、测试

@Autowired
private AdminMapper adminMapper;

@Test
public void testMapper(){
Admin admin = new Admin(null,"tom","123123","汤姆","tom@qq.com",null);
int count = adminMapper.insert(admin);
System.out.println("受影响的行数:"+count);
}

出现问题:
问题1:控制台中文乱码
在这里插入图片描述

问题2:实际插入了两条数据
在这里插入图片描述

问题3:插入的中文出现乱码

乱码的解决方案
1、在父工程中pom.xml文件的标签中加入:

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

2、加入

<build>
<finalName>testweb</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>

插入两条数据解决方案
原因:右键运行@Test方法的时候,会先打包一次。打包的时候会自动测试一遍程序,于是多运行了一次。
解决方案:需要测试时,点击Maven-test插件进行测试

  • 10
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值