1 项目目录
2 所需要的包
3 数据库建表
4 user bean类
package com.db.springmvc.pojo;
public class User {
private Long id;
private String username;
private String password;
public Long getId() {
return id;
}
public void setId(Long 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 [username=" + username + ", password=" + password + "]";
}
}
public class User {
private Long id;
private String username;
private String password;
public Long getId() {
return id;
}
public void setId(Long 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 [username=" + username + ", password=" + password + "]";
}
}
5 user对应的 mapper映射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.db.springmvc.dao.UserDao">
<!-- 取得用户列表 -->
<select id="getUser" resultType="User" parameterType="User">
select
id,
username,
password
From user
where username =#{username} and password
=#{password}
and id=#{id}
</select>
<!-- 新增用户 -->
<insert id="insertUser" parameterType="User">
insert into user(username,password)
values(#{username},#{password})
</insert>
<!-- 修改用户 -->
<update id="updateUser" parameterType="User">
update user
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
</set>
where id=#{id}
</update>
<!-- 删除用户 -->
<delete id="deleteUser" >
delete from user where
id=#{id}
</delete>
</mapper>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.db.springmvc.dao.UserDao">
<!-- 取得用户列表 -->
<select id="getUser" resultType="User" parameterType="User">
select
id,
username,
password
From user
where username =#{username} and password
=#{password}
and id=#{id}
</select>
<!-- 新增用户 -->
<insert id="insertUser" parameterType="User">
insert into user(username,password)
values(#{username},#{password})
</insert>
<!-- 修改用户 -->
<update id="updateUser" parameterType="User">
update user
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
</set>
where id=#{id}
</update>
<!-- 删除用户 -->
<delete id="deleteUser" >
delete from user where
id=#{id}
</delete>
</mapper>
6 dao层接口
package com.db.springmvc.dao;
import java.util.List;
import com.db.springmvc.pojo.User;
public interface UserDao {
public List<User> getUsers();
public User getUser(User user);
public void insertUser(User user);
public int updateUser(User user);
public int deleteUser(int id);
}
import java.util.List;
import com.db.springmvc.pojo.User;
public interface UserDao {
public List<User> getUsers();
public User getUser(User user);
public void insertUser(User user);
public int updateUser(User user);
public int deleteUser(int id);
}
7 service层接口
package com.db.springmvc.service;
import java.util.List;
import com.db.springmvc.pojo.User;
public interface UserService {
public List<User> getUsers() throws Exception;
public User getUserInfo(User user) throws Exception;
public void saveUser(User user) throws Exception;
public int deleteUser(int id) throws Exception;
}
import java.util.List;
import com.db.springmvc.pojo.User;
public interface UserService {
public List<User> getUsers() throws Exception;
public User getUserInfo(User user) throws Exception;
public void saveUser(User user) throws Exception;
public int deleteUser(int id) throws Exception;
}
8 service接口实现类
package com.db.springmvc.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.db.springmvc.dao.UserDao;
import com.db.springmvc.pojo.User;
import com.db.springmvc.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> getUsers() throws Exception {
return userDao.getUsers();
}
@Override
public User getUserInfo(User user) throws Exception {
return userDao.getUser(user);
}
@Override
public void saveUser(User user) throws Exception {
if (user!=null&&user.getId()!=null) {
userDao.updateUser(user);
}else {
userDao.insertUser(user);
}
}
@Override
public int deleteUser(int id) throws Exception {
return userDao.deleteUser(id);
}
}
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.db.springmvc.dao.UserDao;
import com.db.springmvc.pojo.User;
import com.db.springmvc.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> getUsers() throws Exception {
return userDao.getUsers();
}
@Override
public User getUserInfo(User user) throws Exception {
return userDao.getUser(user);
}
@Override
public void saveUser(User user) throws Exception {
if (user!=null&&user.getId()!=null) {
userDao.updateUser(user);
}else {
userDao.insertUser(user);
}
}
@Override
public int deleteUser(int id) throws Exception {
return userDao.deleteUser(id);
}
}
此时注意:@Service
public class UserServiceImpl implements UserService 实现类头加@service注释
@Autowired
private UserDao userDao;属相前加@Autowired 让其自动装配
public class UserServiceImpl implements UserService 实现类头加@service注释
@Autowired
private UserDao userDao;属相前加@Autowired 让其自动装配
8 配置spring的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-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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- 配置DataSource数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="" />
<property name="maxActive" value="5" />
<property name="maxIdle" value="3" />
<property name="maxWait" value="1000" />
<property name="defaultAutoCommit" value="true" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="60" />
</bean>
<!-- 创建SqlSessionFactory,同时指定数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
</beans>
<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-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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- 配置DataSource数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="" />
<property name="maxActive" value="5" />
<property name="maxIdle" value="3" />
<property name="maxWait" value="1000" />
<property name="defaultAutoCommit" value="true" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="60" />
</bean>
<!-- 创建SqlSessionFactory,同时指定数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
</beans>
9 配置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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.db.springmvc.*" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
9 配置user的spring对象xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.db.springmvc.*" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
9 配置user的spring对象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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd" >
<!-- 用户Dao -->
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.db.springmvc.dao.UserDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>
10 配置mybatis的xml
<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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd" >
<!-- 用户Dao -->
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.db.springmvc.dao.UserDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>
10 配置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>
<typeAliases>
<package name="com.db.springmvc.pojo" />
</typeAliases>
<mappers>
<mapper resource="com/db/springmvc/pojo/UserMapper.xml" />
</mappers>
</configuration>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.db.springmvc.pojo" />
</typeAliases>
<mappers>
<mapper resource="com/db/springmvc/pojo/UserMapper.xml" />
</mappers>
</configuration>
11 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<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>
<!-- 监听spring上下文容器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 加载spring的xml配置文件到 spring的上下文容器中 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:*-context.xml</param-value>
</context-param>
<!-- 配置Spring MVC DispatcherServlet -->
<servlet>
<servlet-name>MVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化参数 -->
<init-param>
<!-- 加载SpringMVC的xml到 spring的上下文容器中 -->
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/mvc-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置DispatcherServlet所需要拦截的 url -->
<servlet-mapping>
<servlet-name>MVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/WEB-INF/view/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<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>
<!-- 监听spring上下文容器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 加载spring的xml配置文件到 spring的上下文容器中 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:*-context.xml</param-value>
</context-param>
<!-- 配置Spring MVC DispatcherServlet -->
<servlet>
<servlet-name>MVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化参数 -->
<init-param>
<!-- 加载SpringMVC的xml到 spring的上下文容器中 -->
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/mvc-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置DispatcherServlet所需要拦截的 url -->
<servlet-mapping>
<servlet-name>MVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/WEB-INF/view/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
12 index.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>welcome!</title>
<link rel="stylesheet" type="text/css" href="css/index.css"/>
</head>
<body>
<a href="intoRegist.do">点击这里注册</a>
</body>
</html>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>welcome!</title>
<link rel="stylesheet" type="text/css" href="css/index.css"/>
</head>
<body>
<a href="intoRegist.do">点击这里注册</a>
</body>
</html>
13 regist.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="logo.do" method="post">
<label> <input type="text" name="username" placeholder="用户名" />
<input type="password" name="password" placeholder="密码" />
</label> <input type="submit" value="注册" />
</form>
</body>
</html>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="logo.do" method="post">
<label> <input type="text" name="username" placeholder="用户名" />
<input type="password" name="password" placeholder="密码" />
</label> <input type="submit" value="注册" />
</form>
</body>
</html>
14 编写控制器
package com.db.springmvc.controller;
import javax.servlet.http.HttpServletRequest;
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.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.db.springmvc.pojo.User;
import com.db.springmvc.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("intoRegist")
public ModelAndView index() {
// 创建模型跟视图,用于渲染页面。并且指定要返回的页面为home页面
ModelAndView mav = new ModelAndView("regist");
return mav;
}
@RequestMapping(value="logo",method=RequestMethod.POST)
public ModelAndView regist(HttpServletRequest request,User user){
System.out.println(user);
try {
userService.saveUser(user);
} catch (Exception e) {
e.printStackTrace();
}
return new ModelAndView("index");
}
}
import javax.servlet.http.HttpServletRequest;
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.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.db.springmvc.pojo.User;
import com.db.springmvc.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("intoRegist")
public ModelAndView index() {
// 创建模型跟视图,用于渲染页面。并且指定要返回的页面为home页面
ModelAndView mav = new ModelAndView("regist");
return mav;
}
@RequestMapping(value="logo",method=RequestMethod.POST)
public ModelAndView regist(HttpServletRequest request,User user){
System.out.println(user);
try {
userService.saveUser(user);
} catch (Exception e) {
e.printStackTrace();
}
return new ModelAndView("index");
}
}
最后启动项目输入:
: