1.Maven工程需要导入的jar包
mybatis 3.3.8版本
mysql 5.1.37版本
junit 4.12版本
mybatis-spring 1.3.1版本
spring-webmvc 3.2.8版本
spring-jdbc 3.2.8版本
dbcp 1.4版本
jstl 1.2版本
3.resources下的配置文件
(1)conf文件的配置文件
db.properties
spring-db.xml
spring-mvc.xml
spring-mybatis.xml
spring-service.xml
(2)mapping文件夹下的
userMapper.xml
4.WEB-INF下的文件
(1)inc文件夹下
footer.jsp
header.jsp
left-side.jsp
(2)jsp文件夹下user文件夹下
list.jsp
add.jsp
5.JavaBean类
6.Dao接口类
7.业务层类:
8.控制器类
9.如需要测试类
mybatis 3.3.8版本
mysql 5.1.37版本
junit 4.12版本
mybatis-spring 1.3.1版本
spring-webmvc 3.2.8版本
spring-jdbc 3.2.8版本
dbcp 1.4版本
jstl 1.2版本
2.配置文件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_2_5.xsd" version="2.5">
<display-name>DAY08-01-SSM-Demo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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:conf/spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3.resources下的配置文件
(1)conf文件的配置文件
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/tedustore?useUnicode=true&characterEncoding=utf-8
username=root
password=
initsize=1
maxsize=5
spring-db.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- conf/spring-db.xml 用于管理数据库的连接 -->
<!-- 读取conf/db.properties -->
<util:properties id="dbConfig" location="classpath:conf/db.properties"></util:properties>
<!-- 配置DBCP所需的Bean -->
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="#{dbConfig.driver}"></property>
<property name="url" value="#{dbConfig.url}"></property>
<property name="username" value="#{dbConfig.username}"></property>
<property name="password" value="#{dbConfig.password}"></property>
<property name="initialSize" value="#{dbConfig.initsize}"></property>
<property name="maxActive" value="#{dbConfig.maxsize}"></property>
</bean>
</beans>
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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- conf/spring-mvc.xml 用于管理MVC的配置 -->
<context:component-scan base-package="cn.tedu.store.controller"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
spring-mybatis.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- conf/spring-mybatis.xml 用于管理MVC的配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds"></property>
<property name="mapperLocations" value="classpath:mapping/*.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
<!-- basePackage 设置Mapper(dao)的扫描位置 -->
<property name="basePackage" value="cn.tedu.store.dao"></property>
</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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- conf/spring-service.xml 用于管理业务层 -->
<context:component-scan base-package="cn.tedu.store.service"/>
</beans>
(2)mapping文件夹下的
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="cn.tedu.store.dao.UserDao">
<!-- mapping/userMapper.xml 映射文件 -->
<!-- result: 结果 parameter: 参数 type: 类型
resultType: 用于声明方法的返回结果元素的类型 paramterType: 参数的类型,当方法只有一个参数时候使用!
如果有更多个参数建议使用@Param 注解标注 -->
<select id="findAllUsers" resultType="cn.tedu.store.bean.User">
select id,username,password,mobile,email,create_time as createTime from user limit #{start},#{size}
</select>
<select id="countUsers" resultType="int">
select count(*) from user
</select>
<insert id="insertUser" parameterType="cn.tedu.store.bean.User" useGeneratedKeys="true" keyProperty="id">
insert into user(id,username,password,mobile,email,create_time) values(null,#{username},#{password},#{mobile},#{email},#{createTime})
</insert>
</mapper>
4.WEB-INF下的文件
(1)inc文件夹下
footer.jsp
<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- Main Footer -->
<footer class="main-footer">
<!-- Default to the left -->
<strong>Copyright © 2016 <a href="#">Company</a>.</strong>
</footer>
header.jsp
<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- Main Header -->
<header class="main-header">
<span class="logo">功能列表</span>
<!-- Header Navbar -->
<span class="navbar">
</span>
</header>
left-side.jsp
<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<aside class="main-sidebar">
<section class="sidebar">
<ul class="sidebar-menu" data-widget="tree">
<li class="${userMenu} treeview">
<a><i class="fa fa-user"></i> <span>用户</span>
</a>
<ul class="treeview-menu">
<li class="${usersMenu}"><a href="${base}/user/users.do?page=1"> <i class="fa fa-users"></i> 用户管理</a></li>
<li class="${userAddMenu}"><a href="${base}/user/add.do"> <i class="fa fa-user-plus"></i> 添加用户</a></li>
</ul>
</li>
</ul>
</section>
</aside>
(2)jsp文件夹下user文件夹下
list.jsp
<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="base" scope="request" value="${pageContext.request.contextPath}"/>
<%-- 设置那个左侧菜单是活动的 list.jsp--%>
<c:set var="userMenu" value="active" scope="request"/>
<c:set var="usersMenu" value="active" scope="request"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用户列表</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
font-size: 14px;
}
.main-header{
line-height: 100px;
background-color: #367fa9;
}
.logo{
background-color: #357ca5;
color: #fff;
font-size: 40px;
text-align: center;
width: 230px;
position: relative;
}
.navbar{
background-color: #3c8dbc;
margin-left: 230px;
}
.main-sidebar{
background-color: #222d32;
position: absolute;
width: 230px;
font-size: 14px;
color: #fff;
height:100%;
}
.main-sidebar li{
line-height: 30px;
}
.main-sidebar .treeview-menu a{
text-decoration:none;
color: #3c8dbc;
}
.content-wrapper{
background-color: #fff;
color: #333;
margin-left: 230px;
padding-top: 15px;
padding-left: 30px;
font-size: 14px;
}
.box-title{
display:inline;
}
.content-wrapper li{
display:inline;
}
tr,h1,.box-title{
line-height: 30px;
}
.box-footer{
padding-top: 20px;
}
table{
border-collapse: collapse;
}
th,td{
width: 120px;
padding: 5px;
text-align: center;
border: 1px solid #d2d6de;
}
h1{
font-size: 20px;
}
a{
text-decoration:none;
}
.main-footer{
background: #fff;
border-top: 1px solid #d2d6de;
margin-left: 230px;
font-size: 14px;
padding-top: 30px;
}
.content-wrapper .box-header a,.content-wrapper table a{
padding: 5px;
background-color: #367fa9;
color: #fff;
}
</style>
</head>
<body>
<div>
<!-- 导入导航栏 -->
<c:import url="/WEB-INF/inc/header.jsp"/>
<!-- OK -->
<c:import url="/WEB-INF/inc/left-side.jsp"></c:import>
<div class="content-wrapper">
<section class="content-header">
<h1>用户管理
<small>管理当前系统的全部用户</small>
</h1>
</section>
<!-- Main content -->
<section>
<div>
<div>
<div class="box">
<div class="box-header">
<h3 class="box-title">全部用户</h3>
<a href="${base}/user/add.do">添加</a>
</div>
<!-- /.box-header -->
<div class="box-body">
<table>
<tr>
<th style="width: 10px">#</th>
<th>用户名</th>
<th>邮箱</th>
<th>电话</th>
<th>创建时间</th>
<th></th>
</tr>
<c:forEach items="${users}"
var="user" >
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.email}</td>
<td>${user.mobile}</td>
<td><fmt:formatDate
value="${user.createTime}"
pattern="yy年M月d日 H:m"/></td>
<td>
<a οnclick="return confirm('真的删除 ${user.username}吗?')"
href="${base}/user/delete.do?id=${user.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
</div>
<!-- /.box-body -->
<div class="box-footer">
<ul>
<li><a href="#">«</a></li>
<c:forEach begin="1" end="${pages}"
var="i" >
<li><a href="${base}/user/users.do?page=${i}">${i}</a></li>
</c:forEach>
<li><a href="#">»</a></li>
</ul>
</div>
</div>
<!-- /.box -->
</div>
</div>
</section>
</div>
<!-- Footer -->
<c:import url="/WEB-INF/inc/footer.jsp"></c:import>
</div>
</body>
</html>
add.jsp
<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="base" scope="request" value="${pageContext.request.contextPath}"/>
<%-- 设置那个左侧菜单是活动的 add.jsp--%>
<c:set var="userMenu" value="active" scope="request"/>
<c:set var="userAddMenu" value="active" scope="request"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>添加用户</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
font-size: 14px;
}
.main-header{
line-height: 100px;
background-color: #367fa9;
}
.logo{
background-color: #357ca5;
color: #fff;
font-size: 40px;
text-align: center;
width: 230px;
position: relative;
}
.navbar{
background-color: #3c8dbc;
margin-left: 230px;
}
.main-sidebar{
background-color: #222d32;
position: absolute;
width: 230px;
font-size: 14px;
color: #fff;
height:100%;
}
.main-sidebar li{
line-height: 30px;
}
.main-sidebar .treeview-menu a{
text-decoration:none;
color: #3c8dbc;
}
.content-wrapper{
background-color: #fff;
color: #333;
margin-left: 230px;
padding-top: 15px;
padding-left: 30px;
font-size: 14px;
}
.form-group,h1,.box-title{
line-height: 50px;
}
label{
line-height: 50px;
position: relative;
bottom: 10px;
}
input{
position: absolute;
padding: 5px;
left: 330px;
}
.box-footer{
padding:10px 0;
}
h1{
font-size: 20px;
}
a{
text-decoration:none;
}
.main-footer{
background: #fff;
border-top: 1px solid #d2d6de;
margin-left: 230px;
font-size: 14px;
padding-top: 50px;
}
</style>
</head>
<body>
<div class="wrapper">
<!-- 导入导航栏 -->
<c:import url="/WEB-INF/inc/header.jsp"/>
<!-- OK -->
<c:import url="/WEB-INF/inc/left-side.jsp"></c:import>
<div class="content-wrapper">
<section class="content-header">
<h1>用户管理
<small>管理用户信息</small>
</h1>
</section>
<!-- Main content -->
<section>
<div>
<div>
<div class="box">
<div class="box-header">
<h3 class="box-title">添加用户
<small>${error}</small>
</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<!-- 表单 -->
<!-- form start -->
<form class="form-horizontal"
method="post" action="${base}/user/save.do">
<div class="box-body">
<div class="form-group">
<label for="inputEmail3">用户名</label>
<input type="text" id="inputEmail3" placeholder="用户名" name="username">
</div>
<div class="form-group">
<label for="inputPassword3">密码</label>
<input type="password" id="inputPassword3" placeholder="密码" name="password">
</div>
<div class="form-group">
<label for="inputConfirm">确认密码</label>
<input type="password" id="inputConfirm" placeholder="确认密码" name="confirm">
</div>
<div class="form-group">
<label for="inputEmail">e-mail</label>
<input type="email" id="inputEmail" placeholder="e-mail" name="email">
</div>
<div class="form-group">
<label for="inputMobile">电话</label>
<input type="text" id="inputMobile" placeholder="电话" name="mobile">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit">保存</button>
</div>
<!-- /.box-footer -->
</form>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>
</section>
</div>
<!-- Footer -->
<c:import url="/WEB-INF/inc/footer.jsp"></c:import>
</div>
</body>
</html>
5.JavaBean类
package cn.tedu.store.bean;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private static final long serialVersionUID = -6325206870033939864L;
private Integer id;
private String username;
private String password;
private String mobile;
private String email;
private Date createTime;
public User() {
super();
}
public User(Integer id, String username, String password, String mobile, String email, Date createTime) {
super();
this.id = id;
this.username = username;
this.password = password;
this.mobile = mobile;
this.email = email;
this.createTime = createTime;
}
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;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((createTime == null) ? 0 : createTime.hashCode());
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((mobile == null) ? 0 : mobile.hashCode());
result = prime * result + ((password == null) ? 0 : password.hashCode());
result = prime * result + ((username == null) ? 0 : username.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (createTime == null) {
if (other.createTime != null)
return false;
} else if (!createTime.equals(other.createTime))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (mobile == null) {
if (other.mobile != null)
return false;
} else if (!mobile.equals(other.mobile))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", mobile=" + mobile
+ ", email=" + email + ", createTime=" + createTime + "]";
}
}
6.Dao接口类
package cn.tedu.store.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import cn.tedu.store.bean.User;
public interface UserDao {
/**
* 查询全部的用户信息
* @return 用户信息列表
*/
List<User> findAllUsers(@Param("start") int start,@Param("size") int size);
/**
* 统计用户数量
* @return
*/
int countUsers();
int insertUser(User user);
}
7.业务层类:
package cn.tedu.store.service;
import java.util.List;
import cn.tedu.store.bean.User;
public interface UserService {
/**
* 获取全部用户信息
* @return 用户信息
*/
List<User> list(Integer page);
/**
* 获取用户信息的页数
*/
int listPages();
User save(String username,String password,String mobile,String email);
}
package cn.tedu.store.service.impl;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.tedu.store.bean.User;
import cn.tedu.store.dao.UserDao;
import cn.tedu.store.service.UserService;
@Service("userService") //当前类是业务层组件
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;
public List<User> list(Integer page) {
if (page==null) {
page=1;
}
//计算页面范围
int size=8;
int start=(page-1)*size;
//调用数据层处理业务
return userDao.findAllUsers(start,size);
}
public int listPages() {
int rows = userDao.countUsers();
int size = 8;
int pages = rows/size;
if(rows%size==0) {
return pages;
}
return pages+1;
}
public User save(String username,String password,String mobile,String email) {
User user = new User(null,username,password,mobile,email,new Date());
int n = userDao.insertUser(user);
if (n!=1) {
throw new RuntimeException("添加失败");
}
return user;
}
}
8.控制器类
package cn.tedu.store.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import cn.tedu.store.bean.User;
import cn.tedu.store.service.UserService;
/**
* 控制器,用于处理用户有关的业务功能
*
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/users.do")
public String users(ModelMap map,@RequestParam(required=false,value="page") Integer page) {
//访问业务员获取全部用户信息
List<User> list = userService.list(page);
int pages = userService.listPages();
map.put("users", list);
map.put("pages", pages);
System.out.println(list);
//转发到JSP页面,显示结果
return "user/list";
}
@RequestMapping("/add.do")
public String add() {
return "user/add";
}
@RequestMapping("save.do")
public String save(String username,String password,String mobile,String email) {
User user = userService.save(username,password,mobile,email);
return "redirect:users.do";
}
}
9.如需要测试类
package cn.tedu.test;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyBatisTest {
ClassPathXmlApplicationContext ctx;
@Before
public void init() {
ctx = new ClassPathXmlApplicationContext("conf/spring-db.xml","conf/spring-mybatis.xml");
}
@Test
public void testSqlSession() {
SqlSessionFactory factory = ctx.getBean("sqlSessionFactory", SqlSessionFactory.class);
SqlSession session = factory.openSession();
System.out.println(session);
session.close();
}
}
package cn.tedu.test;
import java.util.Date;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.tedu.store.bean.User;
import cn.tedu.store.dao.UserDao;
public class UserDaoTest {
ClassPathXmlApplicationContext ctx;
UserDao dao;
@Before //在全部测试案例之前执行的方法
public void init() {
ctx = new ClassPathXmlApplicationContext("conf/spring-db.xml","conf/spring-mybatis.xml");
dao = ctx.getBean("userDao", UserDao.class);
}
@After //全部测试案例执行之后执行 destory方法
public void destory() {
ctx.close();
}
@Test
public void testFindAllUsers() {
List<User> list = dao.findAllUsers(0,8);
for (User user : list) {
System.out.println(user);
}
}
@Test
public void testCountUsers() {
int n = dao.countUsers();
System.out.println(n);
}
@Test
public void testInsertUser() {
User user=new User(null, "Andy",
"123", "119", "110@tom.com", new Date());
int n = dao.insertUser(user);
System.out.println(n);
}
}
package cn.tedu.test;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.tedu.store.bean.User;
import cn.tedu.store.service.UserService;
public class UserServiceTest {
ClassPathXmlApplicationContext ctx;
UserService service;
@Before
public void init() {
ctx = new ClassPathXmlApplicationContext("conf/spring-db.xml","conf/spring-mybatis.xml","conf/spring-service.xml");
service = ctx.getBean("userService", UserService.class);
}
@After
public void destory() {
ctx.close();
}
@Test
public void testList() {
List<User> list = service.list(null);
for(User user : list) {
System.out.println(user);
}
}
@Test
public void testListPages() {
int n = service.listPages();
System.out.println(n);
}
@Test
public void testSave() {
User user=service.save("Wang","123", "12345678", "wang@tom.com");
System.out.println(user);
}
}