数据库结构
导入依赖
<?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>org.example</groupId>
<artifactId>ssmbuild</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--Servlet - JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!-- json-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.4</version>
</dependency>
</dependencies>
<!-- 动态导入问题 Mevan资源过滤问题-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
Mevan资源过滤问题
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
链接数据库Datebase
创建 dao controller pojo service 包
resources 创建mybatis-config.xml applicationContext.xml database.propeties
mybatis-config.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
applicationContet.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:spring-mvc.xml"/>
</beans>
配置数据库连接 database.propeties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
在mybatis-config里面配置数据源
<?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>
<!-- 配置数据源-->
<typeAliases>
<package name="com.zhang.pojo"/>
</typeAliases>
<mappers>
<mapper class="com.zhang.dao.UserMapper"/>
</mappers>
</configuration>
创建实体类 定义跟数据库一样 pojo/User
创建接口 dao/UserMapper 里面实现增删改查
package com.zhang.dao;
import com.zhang.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserMapper {
//登录
int loginUser(@Param("username") String username,@Param("password") String password);
//增加一本书
int addUser(User user);
//删除一本书
int deleteUser(@Param("id") int id);
//修改一本书
int updateUser(User user);
//根据id查询一本书
User queryUserById(@Param("id")int id);
//查询所有书
List<User> queryAllUser();
}
创建UserMapper.xml
<?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.zhang.dao.UserMapper">
<!-- 登录-->
<select id="loginUser" resultType="int">
select * from User where username=#{username} and password=#{password}
</select>
<!-- 增加-->
<insert id="addUser" parameterType="User">
insert into User (username,password,usertype) values
(#{username},#{password},#{usertype});
</insert>
<!-- 删除-->
<delete id="deleteUser" parameterType="int">
delete from User where id=#{id}
</delete>
<!-- 修改-->
<update id="updateUser" parameterType="User">
update USer set username=#{username},password=#{password},usertype=#{usertype}
where id=#{id} ;
</update>
<!-- 查询-->
<select id="queryUserById" resultType="User">
select * from User where id=#{id}
</select>
<!-- 查询所有-->
<select id="queryAllUser" resultType="User">
select * from User ;
</select>
</mapper>
创建接口UserService service/UserService
public interface UserService {
//查询
int loginUser(String username, String password);
//增加一本书
int addUser(User user);
//删除一本书
int deleteUser(int id);
//修改一本书
int updateUser(User user);
//根据id查询一本书
User queryUserById(int id);
//查询所有书
List<User> queryAllUser();
}
创建 UserServiceImpl
public class UserServiceImpl implements UserService{
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
//登录
public int loginUser(String username, String password) {
return userMapper.loginUser(username,password);
}
//增加一本书
public int addUser(User user) {
return userMapper.addUser(user);
}
//删除一本书
public int deleteUser(int id) {
return userMapper.deleteUser(id);
}
//修改一本书
public int updateUser(User user) {
return userMapper.updateUser(user);
}
//根据id查询一本书
public User queryUserById(int id) {
return userMapper.queryUserById(id);
}
//查询所有书
public List<User> queryAllUser() {
return userMapper.queryAllUser();
}
}
开始整合spring
创建spring-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置整合mybatis -->
<!-- 1.关联数据库文件 -->
<context:property-placeholder location="classpath:database.properties"/>
<!-- 2.数据库连接池 -->
<!--数据库连接池
dbcp 半自动化操作 不能自动连接
c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)
-->
<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}"/>
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000"/>
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
<!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.zhang.dao"/>
</bean>
</beans>
创建 spring-service.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"
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">
<!-- 扫描service相关的bean -->
<context:component-scan base-package="com.zhang.service" />
<!--BookServiceImpl注入到IOC容器中-->
<bean id="UserServiceImpl" class="com.zhang.service.UserServiceImpl">
<property name="userMapper" ref="userMapper"/>
</bean>
<!-- 声明事务-->
<bean id="transactionMapper" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
<!--JSON乱码问题配置-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.4</version>
</dependency>
添加 WEB模块 创建 lib导包
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">
<!-- 配置DispatcherServlet-->
<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:applicayionContext.xml</param-value>
</init-param>
<!--启动级别-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 乱码过滤-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- session 失效时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
创建spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注解驱动-->
<mvc:annotation-driven/>
<!--静态资源过滤-->
<mvc:default-servlet-handler/>
<!-- 扫描包-->
<context:component-scan base-package="com.zhang.controller"/>
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
创建类UserController
package com.zhang.controller;
import com.zhang.pojo.User;
import com.zhang.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
//controller调用service层
@Autowired
//这是service.xml的name
@Qualifier("UserServiceImpl")
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
//查询
@RequestMapping("/allUser")
public String allUser(Model model){
List<User> users = userService.queryAllUser();
model.addAttribute("users",users);
return "listUser";
}
//跳转
@RequestMapping("/toAddUser")
public String toAddUser(){
return "addUser";
}
//增加
@RequestMapping("/addUser")
public String addUser(User user){
userService.addUser(user);
System.out.println("user+++++"+user);
return "redirect:/user/allUser";
}
//修改跳转
@RequestMapping("/toUpdateUser")
public String toUpdateUser(Model model,int id){
User users = userService.queryUserById(id);
model.addAttribute("user",users);
return "updateUser";
}
//修改
@RequestMapping("/updateUser")
public String updateUser(Model model,User user){
userService.updateUser(user);
User users = userService.queryUserById(user.getId());
model.addAttribute("users",users);
System.out.println("user+++++"+user);
return "redirect:/user/allUser";
}
//删除
@RequestMapping("deleteUser/{id}")
public String deleteUser(@PathVariable("id") int id){
userService.deleteUser(id);
return "redirect:/user/allUser";
}
//登录
@RequestMapping("/loginUser")
public String loginUser(HttpSession session, String username, String password){
int sc=-1;
sc = userService.loginUser(username, password);
System.out.println(username);
session.setAttribute("userLoginInfo",username);
if (sc!=-1){
return "redirect:/user/allUser";
}
return "redirect:/";
// String msg = "";
// if (username != null) {
// if ("admin".equals(username)) {
// msg = "ok";
// } else {
// msg = "用户名有误";
// }
// }
//
// if (password != null) {
// if ("123456".equals(password)) {
// msg = "ok";
// } else {
// msg = "密码有误";
// }
// }
// return msg;
}
@RequestMapping("/goOut")
public String goOut(HttpSession sessionl) {
sessionl.removeAttribute("userLoginInfo");
return "redirect:/";
}
}
登陆页面 idnex.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/loginUser" method="post">
账户:<input type="text" name="username"><br/><br/>
密码:<input type="password" name="password"><br/><br/>
<input type="submit" value="登录">
<input type="reset" value="重置">
</form>
</body>
</html>
l列表页面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>书籍列表</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>书籍列表 —— 显示所有书籍</small>
</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary" href="${pageContext.request.contextPath}/user/toAddUser">新增</a>
</div>
<a class="btn btn-primary" href="${pageContext.request.contextPath}/user/goOut">注销</a>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>用户编号</th>
<th>用户名字</th>
<th>用户密码</th>
<th>用户权限</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="${requestScope.get('users')}">
<tr>
<td>${user.getId()}</td>
<td>${user.getUsername()}</td>
<td>${user.getPassword()}</td>
<td>${user.getUsertype()}</td>
<td>
<a href="${pageContext.request.contextPath}/user/toUpdateUser?id=${user.getId()}">更改</a> |
<a href="${pageContext.request.contextPath}/user/deleteUser/${user.getId()}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
新增页面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>新增用户</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>新增用户</small>
</h1>
</div>
</div>
</div>
<form action="${pageContext.request.contextPath}/user/addUser" method="post">
用户名称:<input type="text" name="username"><br><br><br>
用户密码:<input type="text" name="password"><br><br><br>
用户权限:<input type="text" name="usertype"><br><br><br>
<input type="submit" value="添加">
</form>
</div>
更新页面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>新增用户</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>修改用户</small>
</h1>
</div>
</div>
</div>
<form action="${pageContext.request.contextPath}/user/updateUser" method="post">
<input type="hidden" name="id" value="${user.getId()}"/>
用户名称:<input type="text" name="username" value="${user.getUsername()}"><br><br><br>
用户密码:<input type="text" name="password" value="${user.getPassword()}"><br><br><br>
用户权限:<input type="text" name="usertype" value="${user.getUsertype()}"><br><br><br>
<input type="submit" value="更新">
</form>
</div>