ssm简单整合mybatis-plus(详细步骤分享)
程序员文章站
2022-04-02 21:28:01
ssm整合mybatis-plus一、准备工作1、创建表2、加入依赖3、加入各种配置文件①工程目录②、编写配置文件log4j.propertiesdb.propertiesmybatis的核心配置文件:mybati-config.xmlspring核心配置文件:applicationContext.xmlweb.xml二、正式代码开发1、编写各层①项目工程目录②java代码实体类:Mapper类:EmpMapperService类:EmpServiceService实现类:EmpServiceImplCon...
一、准备工作
开发工具:idea
数据库版本:MySQL8
技术:ssm+mybatisPlus
1、创建表
CREATE TABLE `emp` (
`empno` int NOT NULL AUTO_INCREMENT,
`ename` varchar(10) DEFAULT NULL,
`job` varchar(9) DEFAULT NULL,
`mgr` int DEFAULT NULL COMMENT '雇佣的领导编号',
`hiredate` date DEFAULT NULL COMMENT '雇佣日期',
`sal` int DEFAULT NULL,
`comm` int DEFAULT NULL COMMENT '月薪',
`deptno` int DEFAULT NULL,
PRIMARY KEY (`empno`),
KEY `fk_deptno` (`deptno`),
CONSTRAINT `fk_deptno` FOREIGN KEY (`deptno`) REFERENCES `dept` (`deptno`)
) ENGINE=InnoDB AUTO_INCREMENT=7784 DEFAULT CHARSET=utf8;
2、加入依赖
pom.xml
<build>
<!--同步target目录-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
<!--spring版本-->
<properties>
<spring-version>5.1.6.RELEASE</spring-version>
</properties>
<dependencies>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<!--mybatisPlus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.3</version>
</dependency>
<!--c3p0-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<!-- 支持jstl在jsp的应用 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
3、加入各种配置文件
①工程目录
②、编写配置文件
log4j.properties
加入此文件配置,直接放在resource目录即可,不需要其他地方配置,就能输出日志。(加上很容易找出项目的问题)
log4j.rootLogger=DEBUG, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
db.properties
一定注意MySQL8版本数据库得设置时区
jdbc.driver=com.mysql.cj.jdbc.Driver
#连接后面加上设置时区
jdbc.url=jdbc:mysql:///ssm_mp?serverTimezone=UTC
jdbc.user=root
jdbc.password=root
mybatis的核心配置文件:mybati-config.xml
这里的配置是用于控制台输出SQL语句的。其它配置都被放入了spring的核心配置文件统一管理。
<?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="logImpl" value="LOG4J"/>
</settings>
</configuration>
spring核心配置文件:applicationContext.xml
mybatis的sqlSessionFactory:
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.ssm.entity"/>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
</bean>
注意:mybatisPlus最明显的配置改变就是这里:
<bean class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.ssm.entity"/>
<property name="configLocation" value="classpath:mybatis/mybatis-config.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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!--文件引入-->
<context:property-placeholder location="classpath:db/db.properties"></context:property-placeholder>
<!--数据源-->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--mybatisPlus的SqlSessionFactoryBean-->
<bean class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"/>
<!--别名设置-->
<property name="typeAliasesPackage" value="com.ssm.entity"/>
<!--引入mybatis核心配置文件-->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
</bean>
<!--mapper接口扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.ssm.mapper"/>
</bean>
</beans>
springmvc配置文件:
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<mvc:annotation-driven></mvc:annotation-driven>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--设置前缀-->
<property name="prefix" value="WEB-INF/jsp/"></property>
<!--设置后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
<!--注解扫描-->
<context:component-scan base-package="com.ssm.controller"/>
<context:component-scan base-package="com.ssm.service"/>
</beans>
web.xml
这里有个地方注意下,idea自动生成的是2.4版本,最好改成4.0版本。2.4版本默认不解析jstl的<c:forEach>标签,还需要手动开启
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--加入spring核心配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!--监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置前端控制器DispatcherServlet-->
<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/springmvc.xml</param-value>
</init-param>
<!--启动顺序,值越小越优先-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--注意:这里只有两种你写法:1、【/】2、【/.do】
不能是.*
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--设置字符集过滤器-->
<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>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</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>
二、正式代码开发
1、编写各层
①项目工程目录
②java代码
实体类:
package com.ssm.entity;
import java.util.Date;
public class Emp {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Date hiredate;
private Integer sal;
private Integer comm;
private Integer deptno;
@Override
public String toString() {
return "emp{" +
"empno=" + empno +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", mgr=" + mgr +
", hiredate=" + hiredate +
", sal=" + sal +
", comm=" + comm +
", deptno=" + deptno +
'}';
}
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Integer getMgr() {
return mgr;
}
public void setMgr(Integer mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public Integer getSal() {
return sal;
}
public void setSal(Integer sal) {
this.sal = sal;
}
public Integer getComm() {
return comm;
}
public void setComm(Integer comm) {
this.comm = comm;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
}
Mapper类:EmpMapper
这里就只需要继承BaseMapper接口就能得到父类的大量方法
package com.ssm.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.ssm.entity.Emp;
public interface EmpMapper extends BaseMapper<Emp> {
}
Service类:EmpService
package com.ssm.service;
import com.baomidou.mybatisplus.service.IService;
import com.ssm.entity.Emp;
public interface EmpService extends IService<Emp>{
}
Service实现类:EmpServiceImpl
package com.ssm.service.impl;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ssm.entity.Emp;
import com.ssm.mapper.EmpMapper;
import com.ssm.service.EmpService;
import org.springframework.stereotype.Service;
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
}
Controller:EmpController
由于视图解析器设置了前缀和后缀,这里的setViewName方法,就只需要填写jsp文件名称:emplist。(完整路径是:/WEB-INF/jsp/emplist.jsp
)
package com.ssm.controller;
import com.ssm.entity.Emp;
import com.ssm.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.util.List;
@Controller
public class EmpController {
@Autowired
private EmpService empService;
@RequestMapping("/findEmpList")
public ModelAndView findEmpList(){
List<Emp> empList = empService.selectList(null);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("empList",empList);
modelAndView.setViewName("emplist");
return modelAndView;
}
}
Jsp视图:emplist.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table cellpadding="10" cellspacing="0" border="1">
<tr>
<td>雇员编号</td>
<td>雇员姓名</td>
<td>雇员工作</td>
<td>雇员领导</td>
<td>雇佣日期</td>
<td>雇员月薪</td>
<td>雇员奖金</td>
<td>部门编号</td>
</tr>
<c:forEach items="${empList}" var="li">
<tr>
<td>${li.empno}</td>
<td>${li.ename}</td>
<td>${li.job}</td>
<td>${li.mgr}</td>
<td>${li.hiredate}</td>
<td>${li.sal}</td>
<td>${li.comm}</td>
<td>${li.deptno}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
2、代码测试
页面效果:
控制台输出:
通过页面和控制台输出,可以看出测试通过了。
小结:
通过观察代码,能够发现mybatisPlus给我们的编码带来了很
大便利,单表的各种crud方法都是可以直接拿来就用,而我们只
需要简单的继承和实现框架提供的父类和接口即可。
结语:
前天笔记本重装系统,我用U盘拷贝了笔记,结果重装系统的时候手残,不小心把U盘格式化了,辛辛苦苦做的笔记没了,所以以后直接把笔记上传到这儿更方便!????
本文地址:https://blog.csdn.net/qq_41936090/article/details/110232235
上一篇: 小年轻的冷幽默