Spring+springmvc+mybatis
目录
一.Jar包坐标
Spring+mybatis:mysql驱动,springcontext,mybatis,spring-mybatis
Spring+springmvc:springweb,springwebmvc,jstl+stande,
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- spring相关jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.19</version>
</dependency>
<!-- spring整合mybatis-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.14</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.8.0</version>
</dependency>
<!-- spring整合springmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.19</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.19</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
二.配置spring整合mybatis
mybatis的核心配置文件:别名
Spring整合mybatis的配置:读取properties文件,配置datasource,配置mybatis核心对象,读取mybatis映射文件,开启spring注解的扫描
<!-- 读取properties文件-->
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="location" value="classpath:dataSource.properties"/>
</bean>
<!-- 数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${name}"/>
<property name="password" value="${password}"/>
</bean>
<!-- 配置mybatis核心对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatisConfig.xml"/>
</bean>
<!-- 配置dao-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.a1207.a1207ssmch12.dao"/>
</bean>
<!-- ioc注解扫描-->
<context:component-scan base-package="com.a1207.a1207ssmch12.service"/>
<!-- 配置事务:1.事务管理器-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启注解的声明式事务-->
<tx:annotation-driven/>
三.配置spring整合springmvc
1.Springmvc配置文件:
开启spring注解的扫描,mvc注解开启,视图解析器,静态资源的映射
<!-- ioc注解的扫描-->
<context:component-scan base-package="com.a1207.a1207ssmch12.control"/>
<!-- springmvc的注解的开启-->
<mvc:annotation-driven/>
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
2. Web.xml配置:
servlet,servlet-mappeing,监听器,加载spring配置文件
<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:springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
四. 搭建项目结构
五. 编写代码
注意的是 mysql 如果是很高的版本是不可以用8.几的驱动的,要用的话是必须要加东西的
下面的8.0.29的 mysql-connector-java 要加上时区等等
这个是 5.1.32 的
显示所有
index.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello World!" %>
</h1>
<br/>
<a href="${pageContext.request.contextPath}/all">点击查看</a>
</body>
</html>
dao.UserInfoMapper
package com.A1207.A1207ssm.dao;
import com.A1207.A1207ssm.entity.UserInfo;
import java.util.List;
public interface UserInfoMapper {
public List<UserInfo> queryAll();
}
dao.UserInfoMapper.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.A1207.A1207ssm.dao.UserInfoMapper">
<select id="queryAll" resultType="UserInfo">
SELECT *
FROM userinfo;
</select>
</mapper>
entity.UserInfo
package com.A1207.A1207ssm.entity;
public class UserInfo {
private int id;
private String name;
private String sex;
@Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
service.UserInfoService
package com.A1207.A1207ssm.service;
import com.A1207.A1207ssm.dao.UserInfoMapper;
import com.A1207.A1207ssm.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserInfoService {
@Autowired
UserInfoMapper uim;
public List<UserInfo> selectList(){
return uim.queryAll();
}
}
webapp.WEB-INF.jsp.query.jsp
<%@ page import="com.A1207.A1207ssm.service.UserInfoService" %>
<%@ page import="com.A1207.A1207ssm.entity.UserInfo" %>
<%@ page import="java.util.List" %><%--
Created by IntelliJ IDEA.
User: SSOA
Date: 2022/8/11
Time: 14:39
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</title>
</head>
<body>
<table border="1">
<tr>
<th>id</th>
<th>姓名</th>
<th>密码</th>
</tr>
<c:forEach var="user" items="${userList}">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.sex}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
修改、删除和添加
control.UserInfoControl
package com.A1207.A1207ssm.control;
import com.A1207.A1207ssm.entity.UserInfo;
import com.A1207.A1207ssm.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.web.bind.annotation.RequestParam;
@Controller
public class UserInfoControl {
@Autowired
UserInfoService uis;
// 查询所有
@RequestMapping("/all")
public String all(Model m){
m.addAttribute("userList",uis.selectList());
return "query";
}
// 删除数据
@RequestMapping("/del/{id}")
public String del(@PathVariable int id,Model m){
if (uis.remove(id)>0){
return "redirect:/all";
}else {
m.addAttribute("error","删除失败");
return "redirect:/all";
}
}
// 查询修改数据
@RequestMapping("/toUpdate/{id}")
public String toUpdate(@PathVariable int id,Model m){
m.addAttribute("user",uis.selectOne(id));
return "update";
}
// 修改数据
@RequestMapping("/update")
public String update(UserInfo ui, Model m){
if (uis.update(ui)>0){
return "redirect:/all";
}else {
m.addAttribute("error","修改失败");
return "redirect:/all";
}
}
// 跳转到添加页面
@RequestMapping("/add")
public String add(){
return "add";
}
// 添加数据
@RequestMapping("/addData")
public String addData(UserInfo ui,Model m){
if (uis.addData(ui)>0){
return "redirect:/all";
}else {
m.addAttribute("error","添加失败");
return "redirect:/all";
}
}
}
dao.UserInfoMapper
package com.A1207.A1207ssm.dao;
import com.A1207.A1207ssm.entity.UserInfo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserInfoMapper {
// 查询数据
public List<UserInfo> queryAll();
// 删除数据
public int delete(@Param("id") int id);
// 查询修改的数据
public UserInfo queryOne(@Param("id") int id);
// 修改数据
public int update(UserInfo ui);
// 添加数据
public int addData(UserInfo ui);
}
dao.UserInfoMapper.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.A1207.A1207ssm.dao.UserInfoMapper">
<select id="queryAll" resultType="UserInfo">
SELECT * FROM userinfo;
</select>
<delete id="delete">
delete from userinfo where id = #{id};
</delete>
<select id="queryOne" resultType="UserInfo" parameterType="java.lang.Integer">
SELECT * FROM userinfo where id = #{id};
</select>
<update id="update" parameterType="UserInfo">
update userinfo set `name`=#{name},sex=#{sex} where id = #{id};
</update>
<insert id="addData" parameterType="UserInfo">
insert into userinfo values(null,#{name},#{sex});
</insert>
</mapper>
service.UserInfoService
package com.A1207.A1207ssm.service;
import com.A1207.A1207ssm.dao.UserInfoMapper;
import com.A1207.A1207ssm.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserInfoService {
@Autowired
UserInfoMapper uim;
public List<UserInfo> selectList() {
return uim.queryAll();
}
// 删除数据
public int remove(int id) {
return uim.delete(id);
}
// 查询 修改数据
public UserInfo selectOne(int id) {
return uim.queryOne(id);
}
// 修改数据
public int update(UserInfo userInfo) {
return uim.update(userInfo);
}
// 添加数据
public int addData(UserInfo userInfo) {
return uim.addData(userInfo);
}
}
add.jsp
<%--
Created by IntelliJ IDEA.
User: SSOA
Date: 2022/8/11
Time: 16:57
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>
<form action="${pageContext.request.contextPath}/addData">
姓名: <input type="text" name="name"><br>
性别: <input type="text" name="sex"><br>
<input type="submit" value="添加">
</form>
<div></div>
</body>
</html>
query.jsp
<%@ page import="com.A1207.A1207ssm.service.UserInfoService" %>
<%@ page import="com.A1207.A1207ssm.entity.UserInfo" %>
<%@ page import="java.util.List" %><%--
Created by IntelliJ IDEA.
User: SSOA
Date: 2022/8/11
Time: 14:39
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</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/add">添加数据</a>
<table border="1">
<tr>
<th>id</th>
<th>姓名</th>
<th>密码</th>
<th>操作</th>
</tr>
<c:forEach var="user" items="${userList}">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.sex}</td>
<td>
<a href="del/${user.id}">删除</a>
||
<a href="toUpdate/${user.id}">修改</a>
</td>
</tr>
</c:forEach>
</table>
<div>
${error}
</div>
</body>
</html>
update.jsp
<%--
Created by IntelliJ IDEA.
User: SSOA
Date: 2022/8/11
Time: 16:08
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>
<form action="${pageContext.request.contextPath}/update">
id: <input type="text" name="id" value="${user.id}" readonly><br>
姓名: <input type="text" name="name" value="${user.name}"><br>
性别: <input type="text" name="sex" value="${user.sex}"><br>
<input type="submit" value="修改">
</form>
</body>
</html>