前言
这是一份作业
前期准备
Settings配置
File→Settings(快捷键ALT+CTRL+S)
若无m2文件,则在对应盘新建名为m2的文件夹,拖入repository和settings.xml
新建Project
- 右下角如有弹窗,则选择第二项,自动导入
- 若intellij不停的updating indices,①file→invalidate catches/restart…,重开后重新下载;②file→settings→取消勾选update incices on project open
导入架包
将 Pom_txt 中代码覆盖和
配置Tomcat
JAVA
在 main 下建立 名为 java 和 resources 的 package文件
分别标记两个文件为源代码文件和资源文件
配置resources文件
将 applicationContext.xml、db.xml、string.xml 复制粘贴到resources文件下
applicationContext
<?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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 1.配置数据库相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 2.配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="2"/>
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 扫描bean包 使用别名 -->
<property name="typeAliasesPackage" value="com.cc0701.bean"></property>
<!--配置加载映射文件 UserMapper.xml-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 自动生成dao,mapper-->
<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.cc0701.dao"/>
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--自动扫描-->
<context:component-scan base-package="com.java0701"/>
<!-- 配置事务-->
<!-- 5.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 6.开启事务注解-->
<tx:annotation-driven></tx:annotation-driven>
</beans>
db
jdbc.url=jdbc:mysql://localhost:3306/testtt?useSSL=false
jdbc.user=root
jdbc.password=123456
jdbc.driver=com.mysql.jdbc.Driver
string
<?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.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 1.注解扫描位置-->
<context:component-scan base-package="com.java0701.controller" />
<!-- 2.配置映射处理和适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 3.视图的解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
java
在 java下建立名为 bean controller dao service 的 package文件
在 bean 下建立 User 文件
PS:toString方法和getter setter方法可通过Generate(右键或ALT+INSERT)快速生成
package com.java0701.bean;
public class User {
private int id;
private String name;
private String password;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
在 controller下建立 UserController 文件
package com.java0701.controller;
import com.java0701.bean.User;
import com.java0701.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/login.do")
public ModelAndView login(User user){
boolean flag = userService.login(user.getName(),user.getPassword());
ModelAndView modelAndView = new ModelAndView();
if(flag){
modelAndView.setViewName("../OK");
}else {
modelAndView.setViewName("../Failure");
}
return modelAndView;
}
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(value = "name",defaultValue = "") String name){
ModelAndView modelAndView = new ModelAndView();
List<User> userList = userService.serachByName(name);
System.out.println("userList中的内容为:" + userList);
modelAndView.addObject("userList",userList);
modelAndView.setViewName("../main");
System.out.println("modelAndView中的内容为:" + modelAndView);
return modelAndView;
}
@RequestMapping("/delete.do")
public String delete(int id){
boolean del = userService.delete(id);
if(del){
return "redirect:findAll.do";
}
return "../failure";
}
@RequestMapping("/add,do")
public String add(User user){
boolean ad = userService.add(user);
if(ad){
return "redirect:findAll.do";
}
return "../failure";
}
@RequestMapping("/findById.do")
public String findById(int id, Model model){
User user = userService.findById(id);
model.addAttribute("user",user);
return "../modify";
}
@RequestMapping("/update.do")
public String update(User user){
boolean upd = userService.update(user);
if(upd){
return "redirect:findAll.do";
}
return "../failure";
}
}
在 dao下建立 UserDao文件
package com.java0701.dao;
import com.java0701.bean.User;
public interface UserDao {
User findUserByName(String name);
List<User> findAll();
int deleteById(Integer id);
int add(User user);
int update(User user);
User findUserById(Integer id);
List<User> searchByName(String name);
}
在 service下建立 UserServiece文件
package com.java0701.service;
public interface UserService {
boolean login(String name, String password);
boolean delete(Integer id);
List<User> findAll();
boolean add(User user);
User findById(Integer id);
boolean update(User user);
List<User> serachByName(String name);
}
在 service下建立 Impl package文件
在 Impl下建立 UserServieceImpl文件
package com.java0701.service.Impl;
import com.java0701.bean.User;
import com.java0701.dao.UserDao;
import com.java0701.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public boolean login(String name,String password){
User user = userDao.findUserByName(name);
if(user != null && user.getPassword().equals(password)){
return true;
}
return false;
}
@Override
public boolean delete(Integer id){
int del = userDao.deleteById(id);
if(del>0){
return true;
}
return false;
}
@Override
public List<User> findAll(){
return userDao.findAll();
}
@Override
public boolean add(User user){
int ad = userDao.add(user);
if(ad>0){
return true;
}
return false;
}
@Override
public User findById(Integer id){
return userDao.findUserById(id);
}
@Override
public boolean update(User user){
int upd = userDao.update(user);
if(upd>0){
return true;
}
return false;
}
}
在 JAVA0701下建立 filter文件
在 filter下建立 LoginFilter文件
package com.java0701.filter;
import com.cc0701.bean.User;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
//request.getRequestURL();返回全路径
//request.getRequestURL();返回除去host(域名或者IP)部分的路径
String uri = request.getRequestURI();
System.out.println(uri.indexOf("findAll.do"));
System.out.println(uri.indexOf("login.do"));
if (user==null && uri.indexOf("login.do")==-1){
response.sendRedirect(request.getContextPath()+"/");
}else {
filterChain.doFilter(request,response);
}
}
@Override
public void destroy() {
}
}
web
在webapp下建failure.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/7/1
Time: 20:26
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>失败</title>
</head>
<body>
<h1>失败</h1>
</body>
</html>
在webapp下建ok.jsp
<%--
Created by IntelliJ IDEA.
User: 18235
Date: 2020/7/1
Time: 11:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>成功</title>
</head>
<body>
<h1>成功</h1>
</body>
</html>
在webapp下建index.jsp
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h1>登录</h1>
<form action="/user/login.do" method="post">
name:<input name="name" type="text">
password:<input name="password" type="password">
<input type="submit" value="login">
</form>
</body>
</html>
修改web.jsp
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 配置加载类路径的配置文件 -->
<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>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- 解决中文乱码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 前端控制器(加载classpath:spring-mvc.xml 服务器启动创建servlet) -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
在webapp下建add.jsp
<%--
Created by IntelliJ IDEA.
User: 18235
Date: 2020/7/2
Time: 14:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册</title>
</head>
<body>
<h1>注册</h1>
<div>
<form action="/user/add.do">
<div>
<label for="name">用户名:</label>
<input name="name" type="text" id="name">
</div>
<div>
<label for="password">密码:</label>
<input name="password" type="text" id="password">
</div>
<div>
<input type="submit" value="注册">
</div>
<a href="javascript:window.history.go(-1)">返回</a>
</form>
</div>
</body>
</html>
在webapp下建main.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: 18235
Date: 2020/7/2
Time: 9:22
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>列表</title>
</head>
<body>
<div>
<form action="/user/findAll.do">
<input id="name" type="search" name="name" value="${name}">
<button onclick="form.submit()">搜索</button>
</form>
<table>
<thead>
<tr>
<th>ID</th>
<th>用户名</th>
<th>密码</th>
<th>操作</th>
</tr>
</thead>
<thead>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.password}</td>
<td>
<a href="${pageContext.request.contextPath}/user/delete.do?id=${user.id}">删除</a>
<a href="${pageContext.request.contextPath}/user/findById.do?id=${user.id}">修改</a>
</td>
</tr>
</c:forEach>
</thead>
</table>
</div>
</body>
</html>
在webapp下建modify.jsp
<%--
Created by IntelliJ IDEA.
User: 18235
Date: 2020/7/3
Time: 9:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改</title>
</head>
<body>
<div>
<form action="/user/update.do">
<input name="id" value="${user.id}" type="hidden">
<div>
<label for="name">用户名:</label>
<input name="name" type="text" id="name" value="${user.name}">
</div>
<div>
<label for="password">密码:</label>
<input name="password" type="text" id="password" value="${user.password}">
</div>
<div>
<input type="submit" value="修改">
</div>
<a href="javascript:window.history.go(-1)">返回</a>
</form>
</div>
</body>
</html>