话不多说,ssm框架整合小列子,这次记录一个单表增删改查的ssm例子,所以才手写mapper,平时我都是用****自动生成mapper,简单方便。
需要了解****请看:https://blog.****.net/Destiny_strive/article/details/82915354
步骤
4.添加spring配置文件:applicationContext.xml
5.添加springMVC配置文件:springMVC.xml
9.编写dao层->mapper接口和xml文件 :UserMapper.java、UserMapper.xml
10.编写service层->UserService.java、UserServiceImpl.java
11.编写controller层->UserController.java
12.在jsp文件夹下创建edit.jsp和view.jsp
13.现在配置tomcat运行就能看到user表的列表了,可操作做增删改查
1.创建web Maven项目
创建项目很简单的,不做描述,接下来在pom文件加入项目所需依赖:
<dependencies>
<!--Spring框架核心库 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- Spring Web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!--连接驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!--数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- mybatis ORM框架 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!--mybatis-spring适配器 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!--整合mybatis如果不加这个包会报错 java.lang.NoClassDefFoundError: org/springframewor-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!--测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--引入JSTL标签-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
2.创建数据库配置文件:jdbc.properties
resources目录下添加:jdbc.properties
这里根据自己数据库做相应修改
#数据库配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=admin
3.项目总体目录:
先给出项目总体目录
创建好java目录下的分层的包,resources下创建存放mapper的包,WEB-INF下创建jsp放跳转的页面(下面的配置文件中会配置这些路径)
4.添加spring配置文件:applicationContext.xml
resources目录下添加: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"
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 导入数据库配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 扫描包 这里的包需要根据自己java目录下的包名修改 -->
<context:component-scan base-package="com.demo.service" />
<!-- 配置数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--Mybatis的SessionFactory配置-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.demo.pojo" />
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!--Mybatis的Mapper文件识别-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.demo.dao"/>
</bean>
</beans>
5.添加springMVC配置文件:springMVC.xml
resources目录下添加:springMVC.xml,并且根据文件中‘视图定位’的配置,所以在WEB-INF下创建jsp文件夹
文件内容如下:
<?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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.demo.controller" />
<!--
配置如果没有<mvc:annotation-driven/>,
那么所有的Controller可能就没有解析,所有当有请求时候都没有匹配的处理请求类,
就都去<mvc:default-servlet-handler/>即default servlet处理了。
添加上<mvc:annotation-driven/>后,
相应的do请求被Controller处理,而静态资源因为没有相应的Controller就会被default servlet处理。
总之没有相应的Controller就会被default servlet处理就ok了。
-->
<mvc:annotation-driven />
<!--开通静态资源的访问-->
<mvc:default-servlet-handler />
<!-- 视图定位 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
6.修改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">
<!-- spring的配置文件-->
<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>
<!--中文过滤器-->
<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>
<!-- spring mvc核心:分发servlet -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- spring mvc的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
7.创建数据库相关表
我的jdbc.properties文件中指明了数据库名为test,在test数据库下我创建了一张user表,结构和数据如下:
8.根据数据库表创建pojo类:User.java
pojo的编写规则:数据库字段对应类字段,字段类型要一直 。
package com.demo.pojo;
public class User {
private Integer id;
private String name;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
}
9.编写dao层->mapper接口和xml文件 :UserMapper.java、UserMapper.xml
UserMapper.xml放在resources\mapper下,UserMapper.java放在com.demo.dao这个包下:
注意namespace="com.demo.dao.UserMapper",这里一定写对应的mapper接口具体路径
UserMapper接口的编写规则:
(1)方法名和对应的mapper配置文件中查询语句的id相同
(2)返回类型和resultType的类型一致,没有就是void。
(3)方法中的参数列表中的类型和parameterType一致。
(4)mapper配置文件的namespace对应mapper接口类的全路径。
package com.demo.dao;
import com.demo.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> list();
void del(int id);
void update(User user);
void add(User user);
User get(int id);
}
<?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.demo.dao.UserMapper" >
<select id="list" resultType="user">
select * from user
</select>
<update id="update" parameterType="user">
update user set name=#{name} ,password=#{password} where id=#{id}
</update>
<insert id="add" parameterType="user">
insert into user values (null,#{name},#{password});
</insert>
<delete id="del">
delete from user where id=#{id}
</delete>
<select id="get" resultType="user">
select * from user where id =#{id}
</select>
</mapper>
10.编写service层->UserService.java、UserServiceImpl.java
UserService.java:
package com.demo.service;
import com.demo.pojo.User;
import java.util.List;
public interface UserService {
List<User> list();
void delUser(int id);
void updateUser(User user);
void addUser(User user);
User get(int id);
}
UserServiceImpl.java
package com.demo.service.impl;
import com.demo.dao.UserMapper;
import com.demo.pojo.User;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserMapper userMapper;
public List<User> list() {
return userMapper.list();
}
public void addUser(User user) {
userMapper.add(user);
}
public void delUser(int id) {
userMapper.del(id);
}
public void updateUser(User user) {
userMapper.update(user);
}
public User get(int id) {
return userMapper.get(id);
}
}
11.编写controller层->UserController.java
package com.demo.controller;
import com.demo.pojo.User;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("")
public class UserController {
@Autowired
UserService userService;
@RequestMapping("list")
public String list(Model model){
List<User> us= userService.list();
model.addAttribute("us", us);
return "view";
}
@RequestMapping("add")
public String add(User user,Model model){
userService.addUser(user);
return "redirect:list";
}
@RequestMapping("del")
public String del(int id){
userService.delUser(id);
return "redirect:list";
}
@RequestMapping("editUI")
public String editUI(int id,Model model){
User user = userService.get(id);
model.addAttribute("user",user);
return "edit";
}
@RequestMapping("update")
public String update(User user){
userService.updateUser(user);
return "redirect:list";
}
}
12.在jsp文件夹下创建edit.jsp和view.jsp
view.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
<style>
table,table tr th, table tr td { border:1px solid rgba(41, 36, 35, 0.96); }
#mytable{width:300px;margin: 100px auto}
#add{width:300px;height:500px;margin: 100px auto}
</style>
</head>
<body>
<div id="list">
<table id="mytable">
<thead>
<th>id</th>
<th>名字</th>
<th>密码</th>
<th>修改</th>
<th>删除</th>
</thead>
<tbody>
<c:forEach items="${us}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.password}</td>
<td><a href="editUI?id=${user.id}">edit</a> </td>
<td><a href="del?id=${user.id}">delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<div id="add">
<form action="/add">
姓名:<input type="text" name="name" value=""/><br/>
密码:<input type="text" name="password" value=""/><br/>
<button type="submit">添加</button>
</form>
</div>
</body>
</html>
edit.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<style>
#edit{width:300px;height:500px;margin: 100px auto}
</style>
</head>
<body>
<div id="edit">
<form action="/update">
姓名:<input type="text" name="name" value="${user.name}"/><br/>
密码:<input type="text" name="password" value="${user.password}"/><br/>
<input type="hidden" value="${user.id}" name="id">
<button type="submit" value="">修改</button>
</form>
</div>
</body>
</html>
最后把index.jsp内容修改为:
<%
response.sendRedirect(request.getContextPath()+"/list");
%>