豫ICP备17040950号-2

使用IDEA开发Maven JavaWeb迷你项目

文章目录
  1. 1. 迷你JavaWeb项目
  2. 2. 持久层
    1. 2.1. 新建数据库
    2. 2.2. mybatis的maven配置
    3. 2.3. 连接mysql
    4. 2.4. entity
    5. 2.5. mapper
    6. 2.6. service
    7. 2.7. test
  3. 3. 业务逻辑层
    1. 3.1. spring包
    2. 3.2. applicationContext.xml
    3. 3.3. springmvc-servlet.xml
    4. 3.4. web.xml
    5. 3.5. helloworld
  4. 4. 迷你JavaWeb项目实现
  5. 5. 书签

迷你JavaWeb项目

紧接着《IDEA新建Maven JavaWeb项目》,我们使用Mybatis+Spring+SpringMVC来完成一个迷你项目:
普通用户可以注册登录,管理员可以管理普通用户。

持久层

新建数据库

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;

mybatis的maven配置

在pom.xml中,添加:

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

连接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;
}
}

entity

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

mapper

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

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。

test

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

业务逻辑层

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>

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>

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>

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>

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

迷你JavaWeb项目实现

具体配置和代码实现请自行阅读代码:
https://github.com/voidking/pandawork-start

书签

mybatis文档
http://www.mybatis.org/mybatis-3/zh/index.html

SpringMVC+Spring4+Mybatis3集成,开发简单Web项目+源码下载
http://blog.csdn.net/jiuqiyuliang/article/details/45132493

手把手教你搭建SpringMVC——最小化配置
http://www.cnblogs.com/xing901022/p/5240044.html