<packaging>war</packaging>
<properties>
<servlet.version>3.1.0</servlet.version>
<spring.version>5.2.2.RELEASE</spring.version>
<jsp.version>2.2.1</jsp.version>
<jstl.version>1.2</jstl.version>
<lombok.version>1.18.12</lombok.version>
<mybatis.version>3.4.6</mybatis.version>
<mybatis-spring.version>1.3.2</mybatis-spring.version>
<druid.version>1.1.20</druid.version>
<common.version>4.4.6</common.version>
<mysql.version>5.1.47</mysql.version>
<pagehelper.version>5.1.10</pagehelper.version>
</properties>
在web.xml下配置它,需要同时配置spring配置文件的位置、加载时启动、拦截地址为‘/’(必须为‘/’,这样就不会拦截jsp,防止出现死循环)、utf-8过滤器。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_4_0.xsd"
version="4.0">
<!--配置核心拦截器 这里指定了springmvc.xml的位置 load-on-startup 必须要在最后才行-->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--上面必须是“/”,如果用“ * ”就会连jsp都拦截,最后导致循环-->
<!--配置监听器,让一运行时就读一下applicationContext.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 编码过滤器 解决乱码(设置utf-8)-->
<filter>
<filter-name>charsetFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charsetFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
</web-app>
注解驱动、配置controller扫描包、默认servlet、fastjson消息转化器(前后端分离)、view视图解析
jsp
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/util
http://www.springframework.org/schema/util/spring-util-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/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
">
<!--注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置默认servlet 防止核心拦截器拦截静态资源-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--扫描java下的包,告诉它我将要写·的controller都在这个包下,他只看java包中有controller注解的类,然后看方法-->
<context:component-scan base-package="com.nuedu.controller"></context:component-scan>
<!--view视图解析区-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<!--前缀-->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!--后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
前后端分离(解决跨域)
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/util
http://www.springframework.org/schema/util/spring-util-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/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
">
<!--注解驱动-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="false">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=utf-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--配置默认servlet 防止核心拦截器拦截静态资源-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--解决跨域问题 必须是/** 表示可以无限层-->
在其中配置扫描包,哪些包下类对象放入ioc容器中
applicationContex.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/util
http://www.springframework.org/schema/util/spring-util-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/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
">
<!--扫描java下的包,,他只看java包中有对应注解的,然后将该类加入到IOC中-->
<context:component-scan base-package="com.nuedu.controller"></context:component-scan>
<context:component-scan base-package="com.nuedu.dao"></context:component-scan>
<context:component-scan base-package="com.nuedu.service"></context:component-scan>
<!-- datasource 连接池 id就是这个,别瞎写;注意转义;minIdle是池最小限制,maxActive是池最大;注意property -->
<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://localhost:3306/mybase?useUnicode=true&characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="minIdle" value="8"></property>
<property name="maxActive" value="20"></property>
</bean>
<!-- sqlSessionFactory 第一个参数就是上面配置的连接池的id,表明该工厂是基于druid建立的 -->
<!--参数typeAliasesPackage 配置包的别名 Mapper中pojo中的类不用写前缀了-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"></property>
<property name="typeAliasesPackage" value="com.nuedu.pojo"></property>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="reasonable">true</prop>
<prop key="helperDialect">mysql</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
<!-- 管理 mapper 代理对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.nuedu.dao"></property>
</bean>
</beans>
具体代码见上面web.xml
@Service //服务层
@Component //未命名
@Controller //控制层
@Configuration //配置层
@Repository //仓库
<?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文件的路径和文件名 必须和 dao接口 路径和文件名一致
namespace 必须是dao接口的完全限定名
id 必须对应dao接口中的方法名-->
<!--resultType是泛型T的全限定名,由于前面applicationContext配置了别名,所以只用写类名;例如List<User> 则为User-->
<mapper namespace="com.nuedu.dao.UserDao">
<select id="list" resultType="User" parameterType="User">
select id,username,password from user
<where>
<if test="username != null">
username like concat('%',#{
username},'%')
</if>
</where>
</select>
<insert id="add" parameterType="User">
insert into user(username,password) values(#{
username},#{
password})
</insert>
<delete id="del" parameterType="java.lang.Integer">
delete from user where id=#{
id}
</delete>
<delete id="deee" parameterType="java.lang.Integer">
delete from user where id in
<foreach collection="array" item="item" separator="," open="(" close=")">
#{
item}
</foreach>
</delete>
<select id="getById" parameterType="java.lang.Integer" resultType="User">
select id,username,password from user where id=#{
id}
</select>
<update id="update" parameterType="User">
update user
set
username=#{
username},
password=#{
password}
where id = #{
id}
</update>
</mapper>
package com.nuedu.controller;
import com.nuedu.pojo.User;
import com.nuedu.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.util.List;
@Controller
public class UserController {
@Resource
UserService userService;
@RequestMapping("/list")
String list(User user, ModelMap mo){
if(StringUtils.isBlank(user.getUsername())){
user.setUsername(null);
}else{
mo.addAttribute("username",user.getUsername());
}
List<User> list=userService.list(user);
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
for(User users:list){
System.out.println(users);
}
mo.addAttribute("users",list);
return "user/list";
}
@GetMapping("/add")
String add(){
return "user/edit";
}
@PostMapping("/add")
String add2(User user){
userService.add(user);
return "redirect:list";
}
@GetMapping("/del")
String dele(Integer id){
userService.del(id);
return "redirect:list";
}
@PostMapping("/batchdel")
String deee(Integer[] ids){
userService.deee(ids);
return "redirect:list";
}
@GetMapping("/update")
String goUpdate(Integer id,ModelMap modelMap) {
modelMap.addAttribute("user",userService.getById(id));
return "user/edit";
}
@PostMapping("/update")
String doUpdate(User user) {
userService.update(user);
return "redirect:list";
}
}