最近在复习ssm框架,做了一套比较全的ssm流程项目,工具采用IDEA maven工程
结构:
UserController
package com.atguigu.controller;
import com.atguigu.domain.User;
import com.atguigu.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
/*
*登录
* */
@RequestMapping("login")
public String login(User user){
boolean flag = userService.login(user.getUsername(), user.getPassword());
if (flag) {
return "redirect:/user/findUserAll";
} else {
//相对路径 /WEB-INF/pages/failer.jsp
return "../../failer";
}
}
/*
* 查询所有用户信息 分页加 模糊查询
* */
@RequestMapping("/findUserAll")
public ModelAndView userAll(@RequestParam(name = "page",defaultValue="1")Integer page
,@RequestParam(value = "username",required = false) String username
) {
PageHelper.startPage(page,3);
ArrayList<User> lists = userService.findAll(username);
//pageInfo就是封装好的工具类
PageInfo<User> pageInfo=new PageInfo<>(lists);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("userList", lists);//所有用户信息
modelAndView.addObject("page",pageInfo);//分页显示用户信息
modelAndView.setViewName("user-list");
return modelAndView;
}
/*
* 根据id删除用户信息
* */
@RequestMapping("/deleteById")
public String deleteUser(int id) {
userService.deleteById(id);
return "redirect:/user/findUserAll";
}
/*
* 添加用户信息
* */
@RequestMapping("/addUser")
public String addUser(User user) {
userService.addUser(user);
return "redirect:/user/findUserAll";
}
/*
* 添加用户信息时跳转到添加页面处
* */
@RequestMapping("/skip")
public String skip(){
return "user-add";
}
/*
*根据id查询指定的用户信息
* */
@RequestMapping("/toUpDateById")
public ModelAndView updateUser(int id) {
User user = userService.findUserById(id);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("user", user);
modelAndView.setViewName("user-update");
return modelAndView;
}
/*
* 修改用户信息
* */
@RequestMapping("/updateUser")
public String updateUser(User user) {
userService.updateUser(user);
return "redirect:/user/findUserAll";
}
}
UserDao
package com.atguigu.dao;
import com.atguigu.domain.User;
import org.apache.ibatis.annotations.Param;
import java.util.ArrayList;
//dao层接口
public interface UserDao {
/*
* 根据用户名登录
* */
User findUserByUserName(String username);
/*查询所有用户信息,分页 + 姓名模糊查询
* */
ArrayList<User> findAll(@Param("username") String username);
/*根据指定id删除用户信息
* */
void deleteById(int id);
/*添加用户信息
* */
void addUser(User user);
/*根据id查询指定的用户信息
* */
User findUserById(int id);
/*修改用户信息
* */
void updateUser(User user);
}
User
package com.atguigu.domain;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String username;
private String password;
private Integer page;
private Integer num;
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
UserService
package com.atguigu.service;
import com.atguigu.domain.User;
import org.apache.ibatis.annotations.Param;
import java.util.ArrayList;
//Service层接口
public interface UserService {
/*检查用户是否登录?
* */
boolean login(String username, String password);
/*查询所有用户信息,分页 + 姓名模糊查询
* */
ArrayList<User> findAll(@Param("username") String username);
/*检查根据ID是否删除成功?
* */
void deleteById(int id);
/*检查是否添加用户信息成功?
* */
void addUser(User user);
/*检查根据指定id是否显示指定用户信息?
* */
User findUserById(int id);
/*检查是否删除用户信息成功?
* */
void updateUser(User user);
}
UserServiceImpl
package com.atguigu.service.ImpI;
import com.atguigu.dao.UserDao;
import com.atguigu.domain.User;
import com.atguigu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
//实现service接口
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
/*检查用户是否登录?
* */
@Override
public boolean login(String username, String password) {
User user = userDao.findUserByUserName(username);
if(user !=null && user.getPassword().equals(password)){
return true;
}
return false;
}
/*查询所有用户信息,分页 + 姓名模糊查询
* */
@Override
public ArrayList<User> findAll(String username) {
ArrayList<User> all = userDao.findAll(username);
return all;
}
/*检查根据ID是否删除成功?
* */
@Override
public void deleteById(int id) {
userDao.deleteById(id);
}
/*检查是否添加用户信息成功?
* */
@Override
public void addUser(User user) {
userDao.addUser(user);
}
/*检查根据指定id是否显示指定用户信息?
* */
@Override
public User findUserById(int id) {
User user = userDao.findUserById(id);
return user;
}
/*检查是否删除用户信息成功?
* */
@Override
public void updateUser(User user) {
userDao.updateUser(user);
}
}
UserDao映射配置文件(UserDao.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">
<!--dao接口的全限定类名-->
<mapper namespace="com.atguigu.dao.UserDao">
<!--为dao接口方法提供分sql语句配置-->
<!--登录-->
<select id="findUserByUserName" resultType="user" parameterType="String">
SELECT * FROM user WHERE username=#{username}
</select>
<!--查询所有用户信息 分页+模查询-->
<select id="findAll" resultType="user" parameterType="user">
select * from user
<where>
<if test="username!=null and username!='' ">
and username like '%' #{username} '%'
</if>
</where>
</select>
<!--根据指定id删除用户信息-->
<delete id="deleteById" parameterType="int">
delete from user where id=#{id}
</delete>
<!--添加用户信息-->
<insert id="addUser" parameterType="user">
insert into user(username,password) value(#{username},#{password})
</insert>
<!--根据id查询指定的用户信息-->
<select id="findUserById" resultType="user" parameterType="int">
select * from user where id=#{id}
</select>
<!--修改用户信息-->
<update id="updateUser" parameterType="user">
update user set username=#{username},password=#{password} where id=#{id}
</update>
</mapper>
Spring配置文件(applicationContext.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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 1.创建组件扫描器,支持注解形式-->
<context:component-scan base-package="com.atguigu"/>
<!-- 2.配置数据库相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 3.配置数据源-->
<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>
<!-- 4.创建SqlSessionFactory工厂对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!--扫描bean包,实体类 使用别名-->
<property name="typeAliasesPackage" value="com.atguigu.domain"/>
<!--加载配置映射文件 UserDao.xml-->
<property name="mapperLocations" value="classpath:com/atguigu/dao/*.xml"/>
<!-- 指定全局配置文件的路径-->
<property name="configLocation" value="classpath:mybatis.xml"></property>
</bean>
<!--自动生成dao mapper-->
<!-- 5.配置扫描Dao接口包,动态实现dao接口并且注入到Spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--给出需要扫描的dao接口的包-->
<property name="basePackage" value="com.atguigu.dao"/>
<!--注入sqlSessionFactory工厂对象-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--配置事务-->
<!-- 6.配置事务管理-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 7.开启事务注解-->
<tx:annotation-driven></tx:annotation-driven>
</beans>
数据库配置文件(db.properties)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/tb_user
jdbc.username=root
jdbc.password=123456
日志配置文件(log4j.properties)
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
mybatis全局配置文件(mybatis.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="lazyLoadingEnabled" value="true" />-->
<!-- <!– 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载 –>-->
<!-- <setting name="aggressiveLazyLoading" value="false" />-->
<!-- <!–开启二级缓存–>-->
<!-- <setting name="cacheEnabled" value="true" />-->
<!--开启驼峰命名法-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- <!–实体类别名–>-->
<!-- <typeAliases>-->
<!-- <package name="com.atguigu.domain"/>-->
<!-- </typeAliases>-->
<plugins>
<!--分页插件类所在包-->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
</configuration>
springmvc配置文件(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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--创建组件扫描器,支持注解-->
<context:component-scan base-package="com.atguigu"/>
<!-- 2.配置映射处理和适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--静态资源能访问,但动态映射的请求不能访问-->
<mvc:default-servlet-handler/>
<!--可以保证动态和静态请求都能访问-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- 配置加载类路径的配置文件 -->
<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>
<!--设置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--指定视图解析器配置文件-->
<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>/</url-pattern>
</servlet-mapping>
<!-- 解决中文乱码过滤器 post -->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
登录login.jsp
<%--
Created by IntelliJ IDEA.
User: 李白
Date: 2020/12/21
Time: 21:02
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--绝对路劲--%>
<%
pageContext.setAttribute("ctp",request.getContextPath());
%>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h1>欢迎登录</h1>
<form action="${ctp}/user/login" method="post">
<div class="form-group has-feedback">
<input type="text" name="username" class="form-control"
placeholder="用户名"> <span
class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="password" name="password" class="form-control"
placeholder="密码"> <span
class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-8">
<div class="checkbox icheck">
<label><input type="checkbox"> 记住 下次自动登录</label>
</div>
</div>
<!-- /.col -->
<div class="col-xs-4">
<button type="submit" class="btn btn-primary btn-block btn-flat">登录</button>
</div>
<!-- /.col -->
</div>
</form>
</body>
</html>
错误跳转页面(failer.jsp)
<%--
Created by IntelliJ IDEA.
User: 李白
Date: 2020/12/21
Time: 21:02
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="<%=request.getContextPath()%>/login.jsp">登录失败!请点击返回登录页面</a>
</body>
</html>
展示页面(user-list.jsp)
<%--
Created by IntelliJ IDEA.
User: 李白
Date: 2020/12/21
Time: 20:10
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>用户信息界面</title>
</head>
<body>
<!-- 用户信息界面 -->
<div class="table-box">
<!--工具栏-->
<div class="pull-left">
<div class="form-group form-inline">
<div class="btn-group">
<button type="button" class="btn btn-default" title="新建"
onclick="location.href='${pageContext.request.contextPath}/pages/user-add.jsp'">
<i class="fa fa-file-o"></i> 新建
</button>
<button type="button" class="btn btn-default" title="刷新">
<i class="fa fa-refresh"></i> 刷新
</button>
</div>
</div>
</div>
<%--搜索查询--%>
<form action="${pageContext.request.contextPath}/user/findUserAll" method="post">
查询关键字:<input type="text" name="username" placeholder="请输入用户名" />
<input type="submit" value="搜索"/>
</form>
<!--工具栏/-->
<!--数据列表-->
<table id="dataList"
class="table table-bordered table-striped table-hover dataTable" border="1">
<thead>
<tr>
<th class="" style="padding-right: 0px"><input
id="selall" type="checkbox" class="icheckbox_square-blue">
</th>
<th class="sorting_asc">ID</th>
<th class="sorting_desc">用户名</th>
<th class="sorting_asc sorting_asc_disabled">密码</th>
<th class="text-center">操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${userList}" var="user">
<tr>
<td><input name="ids" type="checkbox"></td>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td class="text-center">
<a href="${pageContext.request.contextPath}/user/toUpDateById?id=${user.id}" class="btn bg-olive btn-xs">更新</a>
<a href="${pageContext.request.contextPath}/user/deleteById?id=${user.id}" class="btn bg-olive btn-xs">删除</a>
<a href="${pageContext.request.contextPath}/user/skip" class="btn bg-olive btn-xs">添加角色</a>
</td>
</tr>
</c:forEach>
<%--分页--%>
<div class="col-md-6">
<p>每页${page.pageSize}条 当前页${page.size}条 / 总${page.pages}页 /
当前页${page.pageNum}
</p>
<a href="${pageContext.request.contextPath}/user/findUserAll?page=${page.pageNum=1}">首页</a>
<a href="${pageContext.request.contextPath}/user/findUserAll?page=${page.pageNum+1}">下一页</a>
<a href="${pageContext.request.contextPath}/user/findUserAll?page=${page.pageNum-1<=1?1:page.pageNum-1}">上一页</a>
<a href="${pageContext.request.contextPath}/user/findUserAll?page=${page.lastPage}">尾页</a>
</div>
</tbody>
<!--
<tfoot>
<tr>
<th>Rendering engine</th>
<th>Browser</th>
<th>Platform(s)</th>
<th>Engine version</th>
<th>CSS grade</th>
</tr>
</tfoot>-->
</table>
<!--数据列表/-->
</div>
<!-- 数据表格 /-->
</body>
</html>
好了,其他的几个页面就不写了,这里提供的差不多了。。