一个计算机技术爱好者与学习者

0%

使用IDEA开发Maven JavaWeb迷你项目

1. 项目简介

本文中,我们使用Mybatis+Spring+SpringMVC来完成一个迷你JavaWeb项目:用户管理系统。
功能:普通用户可以注册登录,管理员可以管理普通用户。

2. 环境准备

2.1. 安装jdk

参考《全平台安装JDK》

2.2. 安装maven

下载地址: http://maven.apache.org/download.cgi

1、解压到自己喜欢的目录(这里郝同学放到D:\Server路径下)。
2、添加环境变量M2_HOME,值为D:\Server\apache-maven-3.3.9
3、在Path中添加;%M2_HOME%\bin;

打开命令提示符,输入mvn -v,如果能够看到maven版本号,说明安装成功。

2.3. 安装tomcat

1、tomcat下载地址:http://tomcat.apache.org/download-80.cgi

2、假设解压目录为D:\Server\apache-tomcat-8.5.9

3、进入目录D:\Server\apache-tomcat-8.5.9\bin,双击startup.bat。

4、浏览器访问http://localhost:8080,启动成功则显示tomcat管理页面。

2.4. idea配置

idea配置参考《IDEA快捷键和配置》

3. 新建Web项目

1、打开idea,File,New,Project,右边导航栏选择Maven,勾选Create from archetype,选择maven-archetype-webapp。

2、Next,输入GroupId为“com.voidking.pandawork”,输入ArtifactId为“pandawork-start”。

3、Next,选择自己安装的Maven中的User settings file,选择settings.xml中配置的Local repository。

4、Next,输入Project name,选择Project location,Finish。

5、稍等几十秒,Web项目便可以创建成功。

4. 初始化项目结构

1、展开pandawork-start项目,展开src文件夹。

2、右键main文件夹,New,Directory,输入directory name为“java”。

3、右键main/java文件夹,New,Package,输入package name为“com.voidking.pandawork”。右键“com.voidking.pandawork”,新建文件README.md。

3、右键main/resources文件夹,New,Directory,输入directory name为“com.voidking.pandawork”。

3、至此,初始化项目结构完成。

5. 持久层

5.1. 新建数据库

1、使用navicat新建mysql数据库,数据库名为pandawork,字符集选择utf8–UTF-8 Unicode,排序规则选择utf8_general_ci。

2、新建表t_user,包括id、username、password三个字段。

1
2
3
4
5
6
7
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`username` varchar(16) NOT NULL,
`password` varchar(16) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

5.2. mybatis的maven配置

在pom.xml中,添加:

1
2
3
4
5
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>

5.3. 连接mysql

1、在pom.xml中,添加:

1
2
3
4
5
6
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>

2、右键resources文件夹,新建文件mybatis-config.xml,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/pandawork"/>
<property name="username" value="root"/>
<property name="password" value="mysql"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/voidking/pandawork/mapper/user.mapper.xml"/>
</mappers>
</configuration>

3、main/java文件夹下,右键com.voidking.pandawork,新建包util。

4、右键包uitl,新建连接数据库的类ConnetDB.java,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.voidking.pandawork.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class ConnectDB {
private static volatile ConnectDB instance=null;
private SqlSessionFactory sqlSessionFactory=null;

private ConnectDB(){
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}

public static ConnectDB getInstance(){
if(instance==null){
synchronized(ConnectDB.class){
if(instance==null){
instance=new ConnectDB();
}
}
}
return instance;
}

public SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
}

5.4. entity

1、main/java文件夹下,右键com.voidking.pandawork,新建包entity。
2、右键包entity,新建类User.java。

5.5. mapper

1、main/java文件夹下,右键com.voidking.pandawork,新建包mapper。右键包mapper,新建接口UserMapper.java。
2、main/resources文件夹下,右键com.voidking.pandawork,新建包mapper。右键包mapper,新建文件user.mapper.xml。

5.6. service

1、main/java文件夹下,右键com.voidking.pandawork,新建包service。
2、右键包service,新建接口UserService.java。
3、右键包service,新建包impl。
4、打开UserService.java,鼠标光标聚焦到“public interface UserService”一行,按下alt+enter(或者选择code,generate),选择com.voidking.pandawork.service.impl包,新建接口实现文件UserServiceImpl.java。

5.7. test

打开UserServiceImpl.java,鼠标光标聚焦到“public class UserServiceImpl implements UserService”一行,按下alt+enter,选择com.voidking.pandawork包,新建测试文件UserServiceTest.java。这样,就在test/java/com.voidking.pandawork包中生成了测试文件。

6. 业务逻辑层

6.1. spring包

在pom.xml中,添加springmvc最小化依赖:

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>

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

6.2. applicationContext.xml

右键main/resources文件夹,新建applicationContext.xml,不配置任何bean,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

</beans>

6.3. springmvc-servlet.xml

右键main/resources文件夹,新建springmvc-servlet.xml,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<!-- 设置使用注解的类所在的jar包 -->
<context:component-scan base-package="com.voidking.pandawork.controller" />
</beans>

6.4. web.xml

打开main/webapp/WEB-INFO/web.xml,修改内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
<!-- 默认是/WEB-INF/applicationContext.xml -->
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
<!-- 默认是/WEB-INF/[servlet名字]-servlet.xml -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

6.5. helloworld

1、main/java文件夹下,右键com.voidking.pandawork,新建包controller。右键包controller,新建类HelloWorld.java,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.voidking.pandawork.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloWorld {
@RequestMapping("/hello")
public @ResponseBody String test() {
return "hello, world! This com from spring!";
}
}

2、部署项目到tomcat的8080端口,访问http://localhost:8080/hello,即可看到“hello, world! This com from spring!”。

7. 迷你JavaWeb项目实现

具体配置和代码实现请自行阅读代码:pandawork-start

8. 书签