1. 创建WEB工程
社区版没有创建web项目的功能,只能创建maven的web工程,这里开始就使用破解的企业版。
1.1 创建步骤
File——New——Project
在弹窗中选中Java Enterprise——project SDK——Java EE version——Application Server
Project SDK:jdk版本
Java EE Version:设置javaEE的版本
Application Server:设置服务器
然后选择We application,如图:
注意:不勾选也可以
选择 Next,输入项目名与工作空间,如图:
点击finish完成创建。
1.2 servlet的demo
1.2.1 index.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/1/15
Time: 21:04
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>zoudmBean</title>
</head>
<body>
<form action="addUser" method="post">
用户名:<input type="text" name="username" /><br>
年龄:<input type="text" name="age" /><br>
<input type="submit" value="提交" />
</form>
</body>
</html>
1.2.2 servlet编写
1. 创建Servlet的步骤
鼠标右键src目录,New——create new Servlet
在弹窗中输入Servlet信息
点击OK,完成创建
2. 编写Servlet
我们发现,在Servlet创建好之后,自动在类上加上了注解@WebServlet(name = "AddUser"),这里我们使用的是4.0版本,Servlet支持注解开发,所以这里我们使用注解的方式开发
1. 添加访问路径
在@WebServlet(name = "AddUser")中加上urlPatterns属性
@WebServlet(name = "AddUser",urlPatterns = "/addUser")
2. servlet代码
package com.bjc.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "AddUser",urlPatterns = "/addUser")
public class AddUser extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置编码
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String userage = request.getParameter("age");
System.out.println("username = " + name + " age = " + userage);
}
}
3. 运行web项目
因为我们之前已经配置了tomcat,所以工具栏会有一个tomcat图标,我们直接点击run即可,如图:
启动成功,自动运行项目
在页面输入相应的信息,点击提交,如图:
java后台执行结果
1.2.3 自定义访问路径
如图:
在弹窗中,选中tomcat Server——修改URL,如图:
再选中Deployment,修改Application Context,如图:
注意:Deployment中的Application Context的访问路径与URL是同步修改的,即修改了Application Context,然后URL会自动更改。
2. 创建Maven的聚合工程
2.1 项目结构
manager
|----------manager pojo(jar)
|----------manager dao(jar)
|----------manager service(jar)
|----------manager web(war)
2.2 创建父工程
1. 点击create project,如图:
2. 选择Maven,勾选create from...,选择site,如图:
3. 点击,next,输入相应的信息
4. 点击next,填写对应的信息之后,finish,如图:
5. 将pom.xml文件自动生成的一些信息删除掉,不删除掉也可以
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bjc</groupId>
<artifactId>manager</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Maven</name>
</project>
2.3 创建子工程
1. 选中父工程manager,点击File——New——Module
2. 在弹窗中,选中Maven,勾选Create from...,选择Quickstart,如图:
注意:因为这里我先创建的是pojo工程,打包方式为jar,所以使用的是quickstart
3. 点击next,填写工程名称,如图:
4. 点击next,按照提示操作即可。
依次创建dao、service、web的子工程,如图:
2.3 在父工程添加依赖管理
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bjc</groupId>
<artifactId>manager</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>manage-pojo</module>
<module>manage-dao</module>
<module>manage-service</module>
<module>manage-web</module>
</modules>
<!--可以删除,值自定义,意义是在idea中右侧的Maven中作为显示用的,默认是项目名-->
<name>manage</name>
<!--定义jar版本-->
<properties>
<junit.version>4.12</junit.version>
<spring.version>4.1.3.RELEASE</spring.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis.spring.version>1.2.2</mybatis.spring.version>
<mysql.version>5.1.32</mysql.version>
<druid.version>1.0.9</druid.version>
<jstl.version>1.2</jstl.version>
<servlet.version>2.5</servlet.version>
<tomcat.version>2.2</tomcat.version>
<jsp.version>2.0</jsp.version>
</properties>
<!-- 依赖管理 -->
<dependencyManagement>
<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--mybatis与spring整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<!--数据库相关-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--JSP与Servlet-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!--配置插件-->
<build>
<!--配置资源拷贝插件-->
<resources>
<!--配置main下的文件-->
<resource>
<directory>src/main/java</directory>
<includes>
<!--将main下及其子目录下的.xml文件拷贝-->
<include>**/*.xml</include>
</includes>
</resource>
<!--配置resources下的文件-->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
<!--配置tomcat插件(父工程中只做声明)-->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${tomcat.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2.4 在子工程中添加依赖
2.4.1 Dao工程添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>manager</artifactId>
<groupId>com.bjc</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>manage-dao</artifactId>
<name>manage-dao</name>
<dependencies>
<!--dao需要依赖pojo-->
<dependency>
<groupId>com.bjc</groupId>
<artifactId>manage-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--添加其他依赖-->
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<!--mybatis与spring整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<!--数据库相关-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
</dependencies>
</project>
2.4.2 在service工程中添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>manager</artifactId>
<groupId>com.bjc</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>manage-service</artifactId>
<name>manage-service</name>
<dependencies>
<!--service需要依赖Dao工程-->
<dependency>
<groupId>com.bjc</groupId>
<artifactId>manage-dao</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
</dependencies>
</project>
2.4.3 添加web工程的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>manager</artifactId>
<groupId>com.bjc</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>manage-web</artifactId>
<packaging>war</packaging>
<dependencies>
<!-- web工程依赖service -->
<dependency>
<groupId>com.bjc</groupId>
<artifactId>manage-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!--JSP与Servlet-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
</dependencies>
<!--添加插件-->
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<path>/</path>
<port>9090</port>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.4 开始测试代码
2.4.1 pojo定义
package com.bjc.pojo;
public class User {
private Integer id;
private String username;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2.4.2 Mapper
接口:
package com.bjc.mapper;
import com.bjc.pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> getUsers();
}
配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjc.mapper.UserMapper">
<select id="getUsers" resultType="com.bjc.pojo.User">
select * from user
</select>
</mapper>
2.4.3 在web工程创建测试类
我们在引入依赖的时候,将Junit的依赖在web层引入了,所以,我们的测试类都在web工程中进行。
1. 补全maven工程目录
在main中新建java与resources目录
在src下新建test目录
2. 添加配置文件
在web工程的resource目录下新建三个目录,如图:
2.1 mybatis目录文件
mybatisConfig.xml
<?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>
<settings>
<!-- 打印查询语句 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
</configuration>
2.2 resource目录
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
2.3 spring目录
applicationContext_action.xml
<?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"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<context:component-scan base-package="com.bjc.web"></context:component-scan>
</beans>
applicationContext_biz.xml
<?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"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<context:component-scan base-package="com.bjc.service"/>
<!-- <bean id="depBiz" class="cn.bjc.biz.impl.DepBiz"></bean> -->
</beans>
applicationContext_tx.xml
<?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"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- dataSource定义在dao.xml中,所以这里找不到,因此需要引入 -->
<import resource="classpath:spring/applicationContext_dao.xml"></import>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="do*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceMethod" expression="execution(* com.bjc.service.impl.*.*(..))"/>
<aop:advisor pointcut-ref="serviceMethod" advice-ref="advice" />
</aop:config>
</beans>
applicationContext_dao.xml
<?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"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 配置解析properties文件的工具类 -->
<context:property-placeholder location="classpath:resource/*.properties"></context:property-placeholder>
<!-- 自动扫描 -->
<context:component-scan base-package="com.bjc.mapper"/>
<!-- 定义数据源Bean -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="maxActive" value="10"/>
<property name="minIdle" value="3"/>
</bean>
<!-- 加载mybatis核心配置文件 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 核心配置文件 -->
<property name="configLocation" value="classpath:mybatis/mybatisConfig.xml"></property>
<!-- 配置别名包扫描 name为typeAliasesPackage value为pojo的包名-->
<property name="typeAliasesPackage" value="com.bjc.pojo"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bjc.mapper" />
</bean>
</beans>
springMvc.xml
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 启动自动扫描 -->
<context:component-scan base-package="com.bjc.web" />
<!-- 注册MVC注解驱动 -->
<mvc:annotation-driven />
<!-- 静态资源可访问的设置方式 -->
<mvc:default-servlet-handler />
<!-- 配置视图解析器,可以显式设置,也可以不设置,不设置会依据SpringMVC的默认设置 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置静态资源映射 -->
<mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
</beans>
3. 编写测试类
package com.bjc.test;
import com.bjc.mapper.UserMapper1;
import com.bjc.pojo.User;
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 java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext_dao.xml")
public class ManageTest {
@Autowired
private UserMapper1 userMapper1;
@Test
public void test(){
List<User> list = userMapper1.getUsers();
System.out.println(list.toString());
}
}
运行结果正确,到这里说明整合完成。