Java学习路线-65:整合 SSM-Spring+SpringMVC+MyBatis

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: Java学习路线-65:整合 SSM-Spring+SpringMVC+MyBatis

整合 SSM

项目结构

.
├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── pengshiyu
        │           ├── controller
        │           │   └── UserController.java
        │           ├── dao
        │           │   ├── UserDao.java
        │           │   └── impl
        │           │       └── UserDaoImpl.java
        │           ├── interceptor
        │           │   └── MyInterceptor.java
        │           ├── service
        │           │   ├── UserService.java
        │           │   └── impl
        │           │       └── UserServiceImpl.java
        │           └── vo
        │               └── User.java
        ├── resources
        │   ├── applicationContext.xml
        │   ├── db.properties
        │   ├── mappers
        │   │   └── user.mapper.xml
        │   ├── mybatis-config.xml
        │   └── spring-mvc.xml
        └── webapp
            ├── WEB-INF
            │   └── web.xml
            ├── detail.jsp
            ├── edit.jsp
            └── list.jsp

依赖

pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>spring-mvc-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <!-- tomcat7插件 maven 命令 tomcat7:run 启动项目-->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>8080</port>
                    <path>/</path>
                    <uriEncoding>UTF-8</uriEncoding>
                    <!--添加忽略war包检查标签,则可以让tomcat7:run指令正常启动tomcat-->
                    <ignorePackaging>true</ignorePackaging>
                    <contextFile>src/main/webapp/WEB-INF/web.xml</contextFile>
                    <contextReloadable>true</contextReloadable>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- 文件上传-->
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.4</version>
        </dependency>
        <!-- JSON-->
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.11.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.11.0</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.5</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.4</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>
    </dependencies>
</project>

配置

1、web.xml


<?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:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Spring MVC-->
    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!-- 表示容器在启动时立即加载servlet -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <!-- 处理所有URL -->
        <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>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

2、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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx.xsd
">
    <!-- 读取数据库配置-->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:db.properties"/>
    </bean>
    <!-- 配置dataSource-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>
    <!-- 配置工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    <!-- 申明式事务-->
    <!-- 事务管理器-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="save" propagation="REQUIRED"/>
            <tx:method name="get" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.pengshiyu.service.impl.*.* (..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
    </aop:config>
    <!-- 扫描包下的注解-->
    <context:component-scan base-package="com.pengshiyu"/>
</beans>

3、db.properties


driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/data
username=root
password=123456

4、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"
       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:component-scan base-package="com.pengshiyu.controller"/>
</beans>

5、mybatis-config.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.pengshiyu.vo"/>
    </typeAliases>
    <mappers>
        <mapper resource="mappers/user.mapper.xml"/>
    </mappers>
</configuration>

6、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.pengshiyu.vo.user.mapper">
    <!--
    创建users表
    create table users(
        id int PRIMARY key auto_increment,
        name varchar(20),
        password varchar(20)
    )
    插入测试数据
    insert into users(name, `password`) values("小明", "123456");
    insert into users(name, `password`) values("小王", "123457");
    insert into users(name, `password`) values("小赵", "123458");
    insert into users(name, `password`) values("小四", "123459");
    -->
    <select id="selectList" resultType="User">
        select * from users
    </select>
    <select id="selectOne" resultType="User">
        select * from users where id = #{id}
    </select>
    <!-- 自增主键封装到了对象的id属性上 -->
    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into users(name, password) values(#{name}, #{password})
    </insert>
    <delete id="delete">
        delete from users where id = #{id}
    </delete>
    <update id="update">
        update users set name = #{name}, password = #{password} where id = #{id}
    </update>
</mapper>

实体类

package com.pengshiyu.vo;
public class User {
    private int id;
    private String name;
    private String password;
    public User(int id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }
    public User() {
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
package com.pengshiyu.dao;
import com.pengshiyu.vo.User;
import java.util.List;
public interface UserDao {
    public List<User> list();
    public User detail(int id);
    public int delete(int id);
    public int update(User user);
    public int insert(User user);
}

实现类


package com.pengshiyu.dao.impl;
import com.pengshiyu.dao.UserDao;
import com.pengshiyu.vo.User;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("userDao")
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
    @Autowired
    @Override
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        super.setSqlSessionFactory(sqlSessionFactory);
    }
    public List<User> list() {
        return getSqlSession().selectList("com.pengshiyu.vo.user.mapper.selectList");
    }
    public User detail(int id) {
        return getSqlSession().selectOne("com.pengshiyu.vo.user.mapper.selectOne", id);
    }
    public int delete(int id) {
        return getSqlSession().delete("com.pengshiyu.vo.user.mapper.delete", id);
    }
    public int update(User user) {
        return getSqlSession().update("com.pengshiyu.vo.user.mapper.update", user);
    }
    public int insert(User user) {
        return getSqlSession().insert("com.pengshiyu.vo.user.mapper.insert", user);
    }
}

Service

package com.pengshiyu.service;
import com.pengshiyu.vo.User;
import java.util.List;
public interface UserService {
    public List<User> list();
    public User detail(int id);
    public int delete(int id);
    public int update(User user);
    public int insert(User user);
}

实现类


package com.pengshiyu.service.impl;
import com.pengshiyu.dao.UserDao;
import com.pengshiyu.service.UserService;
import com.pengshiyu.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public List<User> list() {
        return userDao.list();
    }
    public User detail(int id) {
        return userDao.detail(id);
    }
    public int delete(int id) {
        return userDao.delete(id);
    }
    public int update(User user) {
        return userDao.update(user);
    }
    public int insert(User user) {
        return userDao.insert(user);
    }
}

Controller

package com.pengshiyu.controller;
import com.pengshiyu.service.UserService;
import com.pengshiyu.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.io.IOException;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    @RequestMapping("/list")
    public String list(ModelMap modelMap) throws IOException {
        List<User> list = userService.list();
        System.out.println(list);
        modelMap.addAttribute("list", list);
        return "/list.jsp";
    }
    @RequestMapping("/detail")
    public String detail(int id, ModelMap modelMap) throws IOException {
        User user = userService.detail(id);
        modelMap.addAttribute("user", user);
        return "/detail.jsp";
    }
    @RequestMapping("/update")
    public String update(int id, ModelMap modelMap) throws IOException {
        User user = userService.detail(id);
        modelMap.addAttribute("title", "修改");
        modelMap.addAttribute("user", user);
        return "/edit.jsp";
    }
    @RequestMapping("/add")
    public String add(ModelMap modelMap) throws IOException {
        User user = new User();
        modelMap.addAttribute("title", "添加");
        modelMap.addAttribute("user", user);
        return "/edit.jsp";
    }
    @RequestMapping("/delete")
    public String delete(int id) throws IOException {
        userService.delete(id);
        return "redirect:list";
    }
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String edit(User user, ModelMap modelMap) throws IOException {
        if (user.getId() != 0) {
            userService.update(user);
        } else {
            userService.insert(user);
        }
        return "redirect:detail?id=" + user.getId();
    }
}

拦截器

此项目中并没有配置生效


package com.pengshiyu.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
public class MyInterceptor implements HandlerInterceptor {
    private List<String> allowedUrls;
    // 在请求处理的方法执行之前执行,
    // 返回true执行下一个拦截器,
    // 返回false不执行下一个拦截器
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle");
        // 解决中文打印乱码输出
        response.setContentType("text/html;charset=utf-8");
        // 判断session
        Object user = request.getSession().getAttribute("user");
        if(user != null){
            return true;
        }
        // 判断放行路径
        String url = request.getRequestURL().toString();
        System.out.println(url);
        for(String temp: allowedUrls){
            if(url.endsWith(temp)){
                return true;
            }
        }
        // 如果没有登录就重定向到登录页面
        response.sendRedirect("/login.do");
        return false;
    }
    // 在处理方法执行之后执行
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
    }
    // 在DispatcherServlet 之后执行 清理工作
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion");
    }
    public List<String> getAllowedUrls() {
        return allowedUrls;
    }
    public void setAllowedUrls(List<String> allowedUrls) {
        this.allowedUrls = allowedUrls;
    }
}

页面

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%> <%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<h2>list</h2>
<button>
  <a href="/user/add">添加</a>
</button>
<table>
  <tr>
    <th>编号</th>
    <th>姓名</th>
    <th>密码</th>
    <th>详细</th>
    <th>删除</th>
  </tr>
  <c:forEach items="${list}" var="item">
    <tr>
      <td>${item.id}</td>
      <td>${item.name}</td>
      <td>${item.password}</td>
      <td><a href="/user/detail?id=${item.id}">详细</a></td>
      <td><a href="/user/update?id=${item.id}">修改</a></td>
      <td><a href="/user/delete?id=${item.id}">删除</a></td>
    </tr>
  </c:forEach>
</table>

detail.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%> <%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<h2>detail</h2>
<p>
  <a href="/user/list">返回列表</a>
</p>
<button><a href="/user/update?id=${user.id}">编辑</a></button>
<p>id: ${user.id}</p>
<p>name: ${user.name}</p>
<p>password: ${user.password}</p>

edit.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%> <%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<h2>${title}</h2>
<p>
  <a href="/user/list">返回列表</a>
</p>
<form action="/user/edit" method="POST">
  <input type="text" name="id" value="${user.id}" hidden />
  <p>
    <label for="name">name</label>
    <input id="" type="text" name="name" value="${user.name}" />
  </p>
  <p>
    <label for="password">password</label>
    <input type="text" name="password" value="${user.password}" />
  </p>
  <p>
    <input type="submit" />
  </p>
</form>

项目代码

https://gitee.com/mouday/spring-springmvc-mybatis-demo

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
14天前
|
缓存 Java 数据库连接
深入探讨:Spring与MyBatis中的连接池与缓存机制
Spring 与 MyBatis 提供了强大的连接池和缓存机制,通过合理配置和使用这些机制,可以显著提升应用的性能和可扩展性。连接池通过复用数据库连接减少了连接创建和销毁的开销,而 MyBatis 的一级缓存和二级缓存则通过缓存查询结果减少了数据库访问次数。在实际应用中,结合具体的业务需求和系统架构,优化连接池和缓存的配置,是提升系统性能的重要手段。
31 4
|
14天前
|
SQL Java 数据库连接
spring和Mybatis的各种查询
Spring 和 MyBatis 的结合使得数据访问层的开发变得更加简洁和高效。通过以上各种查询操作的详细讲解,我们可以看到 MyBatis 在处理简单查询、条件查询、分页查询、联合查询和动态 SQL 查询方面的强大功能。熟练掌握这些操作,可以极大提升开发效率和代码质量。
27 3
|
19天前
|
Java 数据库连接 数据库
spring和Mybatis的逆向工程
通过本文的介绍,我们了解了如何使用Spring和MyBatis进行逆向工程,包括环境配置、MyBatis Generator配置、Spring和MyBatis整合以及业务逻辑的编写。逆向工程极大地提高了开发效率,减少了重复劳动,保证了代码的一致性和可维护性。希望这篇文章能帮助你在项目中高效地使用Spring和MyBatis。
10 1
|
2月前
|
Java 数据库连接 Maven
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和MyBatis Generator,使用逆向工程来自动生成Java代码,包括实体类、Mapper文件和Example文件,以提高开发效率。
135 2
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。
|
2月前
|
搜索推荐 Java 数据库连接
Java|在 IDEA 里自动生成 MyBatis 模板代码
基于 MyBatis 开发的项目,新增数据库表以后,总是需要编写对应的 Entity、Mapper 和 Service 等等 Class 的代码,这些都是重复的工作,我们可以想一些办法来自动生成这些代码。
36 6
|
2月前
|
Java 关系型数据库 MySQL
springboot学习五:springboot整合Mybatis 连接 mysql数据库
这篇文章是关于如何使用Spring Boot整合MyBatis来连接MySQL数据库,并进行基本的增删改查操作的教程。
153 0
springboot学习五:springboot整合Mybatis 连接 mysql数据库
|
2月前
|
Java 数据库连接 API
springBoot:后端解决跨域&Mybatis-Plus&SwaggerUI&代码生成器 (四)
本文介绍了后端解决跨域问题的方法及Mybatis-Plus的配置与使用。首先通过创建`CorsConfig`类并设置相关参数来实现跨域请求处理。接着,详细描述了如何引入Mybatis-Plus插件,包括配置`MybatisPlusConfig`类、定义Mapper接口以及Service层。此外,还展示了如何配置分页查询功能,并引入SwaggerUI进行API文档生成。最后,提供了代码生成器的配置示例,帮助快速生成项目所需的基础代码。
128 1
|
2月前
|
Java 数据库连接 Maven
Spring整合Mybatis
Spring整合Mybatis
|
XML Java 数据库连接
Java框架-MyBatis三剑客之MyBatis Generator(mybatis-generator MBG插件)详解(下)
Java框架-MyBatis三剑客之MyBatis Generator(mybatis-generator MBG插件)详解(下)
250 0
Java框架-MyBatis三剑客之MyBatis Generator(mybatis-generator MBG插件)详解(下)
|
Java 数据库连接 Maven
Java框架-MyBatis三剑客之MyBatis Generator(mybatis-generator MBG插件)详解(上)
Java框架-MyBatis三剑客之MyBatis Generator(mybatis-generator MBG插件)详解(上)
163 0
Java框架-MyBatis三剑客之MyBatis Generator(mybatis-generator MBG插件)详解(上)