码迷,mamicode.com
首页 > 编程语言 > 详细

springboot整合mybatis增删查改

时间:2021-06-17 17:15:52      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:rri   request   zone   int   demo   扫描   mysqld   rest   osi   

1.选择依赖

SpringWeb,JDBC API, MybatisFramework, MysqlDriver

2.application.yml

将 application.properties改为application.yml(使用更简洁), application.yml文件内容如下:

server:
 port: 8081
?
spring:
 datasource:
   username: root
   password: root
   url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
?
mybatis:
 mapper-locations: classpath:mapping/*Mapper.xml
 type-aliases-package: com.example.entity.demo
 
logging:
 level:
   com:
     example:
       mapper : debug

3.pom.xml

在build中配置resources,来防止我们资源导出失败的问题-->

<build>
   <resources>
       <resource>
           <directory>src/main/resourcesdirectory>
           <includes>
               <include>**/*.properties
               **/*.xmlinclude>
           includes>
           <filtering>truefiltering>
       resource>
       <resource>
           <directory>src/main/javadirectory>
           <includes>
               <include>**/*.properties

               **/*.xmlinclude>
           includes>
           <filtering>truefiltering>
       resource>
   resources>
build>

4.创建包和类

controller包UserController类, entity包User类, mapper包UserMapper(interfac接口), service包UserService类,mapping包UserMapping.xml

技术图片

5.实体类

实体类User类里声明变量,有参构造,Set()、Get()方法, 以及toString()

6.接口UserMapper

package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
?
import java.util.List;
?
/**
* @author x
* @Date 2021/05/24
*/
@Repository
public interface UserMapper {
//   查询全部
   List<User>getAll();
?
// 按id查
   User Sel(@Param("user")User user);
// 增
   int Add(@Param("user")User user);
// 改
   int Update(@Param("user")User user);
// 删
   int Delete(@Param("user")User user);
}

7.UserMapper.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.example.demo.mapper.UserMapper">
?
   <resultMap id="BaseResultMap" type="com.example.demo.entity.User">
       <result column="id" jdbcType="INTEGER" property="id"/>
       <result column="userName" jdbcType="VARCHAR" property="username"/>
       <result column="passWord" jdbcType="VARCHAR" property="password"/>
       <result column="realName" jdbcType="VARCHAR" property="address"/>
   resultMap>

   <select id="getAll" resultType="com.example.demo.entity.User">
       select * from user
   select>

   <select id="Sel" resultType="com.example.demo.entity.User">
       select * from user where 1=1
       <if test="user.id != null">
           AND id = #{user.id}
       if>
   select>

   <insert id="Add" parameterType="com.example.demo.entity.User">
       INSERT INTO user
       <trim prefix="(" suffix=")" suffixOverrides=",">
           <if test="user.username != null">
               username,
           if>
           <if test="user.password != null">
               password,
           if>
           <if test="user.address != null">
               address,
           if>
       trim>
       <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
           <if test="user.username != null">
               #{user.username,jdbcType=VARCHAR},
           if>
           <if test="user.password != null">
               #{user.password,jdbcType=VARCHAR},
           if>
           <if test="user.address != null">
               #{user.address,jdbcType=VARCHAR},
           if>
       trim>
   insert>

   <update id="Update" parameterType="com.example.demo.entity.User">
       UPDATE user
       <set>
           <if test="user.username != null">
               username = #{user.username},
           if>
           <if test="user.password != null">
               password = #{user.password},
           if>
           <if test="user.address != null">
               address = #{user.address},
           if>
       set>
       WHERE
       id=#{user.id}
   update>

   <delete id="Delete"  parameterType="com.example.demo.entity.User">
       DELETE FROM user WHERE id = #{user.id}
   delete>
?
mapper>

8.UserService类

 /*
       Service层介于controller和dao之间作为服务层进行一些逻辑处理,
       这里逻辑太简单所以知识单纯调用dao所以不做注释
    */
package com.example.demo.service;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
?
/**
* @author x
* @Date 2021/05/24
*/
@Service
public class UserService {
   @Autowired
   UserMapper userMapper;
//查询全部
   public List<User>getAll(){
       return userMapper.getAll();
  }
//根据id查询
   public User sel(User user){
       return userMapper.Sel(user);
  }
?
   public  String Add(User user){
       int a = userMapper.Add(user);
       if (a==1){
           return "添加成功";
      }else {
           return "添加失败";
      }
?
  }
?
   public String Update(User user){
       int a = userMapper.Update(user);
       if (a == 1) {
           return "修改成功";
      }else {
           return "修改失败";
      }
  }
?
   public String Delete(User user){
       int a = userMapper.Delete(user);
       if (a == 1) {
           return "删除成功";
      }else {
           return "删除失败";
      }
  }
?
}

9.UserController类

package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @author x
* @Date 2021/05/24
*/
@RestController
@RequestMapping("/test")
public class UserController {
?
   @Autowired
   private UserService userService;
//查询全部
   @RequestMapping(value = "/select", produces = "application/json;charset=UTF-8", method = RequestMethod.GET )
   @ResponseBody
   public String getAll(User user){
       return userService.getAll().toString();
  }
//根据id查询
   @RequestMapping(value = "/selectUserByid", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
   @ResponseBody
   public String GetUser(User user){
       return userService.sel(user).toString();
  }
   
   @RequestMapping(value = "/add", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
   public String Add(User user){
       return userService.Add(user);
  }
?
   @RequestMapping(value = "/update", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
   public String Update(User user){
       return userService.Update(user);
  }
?
   @RequestMapping(value = "/delete", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
   public String Delete(User user){
       return userService.Delete(user);
  }
}

10.DemoApplication测试类

package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author x
* @Date 2021/05/24
*/
@MapperScan("com.example.demo.mapper") //扫描的mapper
@SpringBootApplication
public class DemoApplication {
?
   public static void main(String[] args) {
       SpringApplication.run(DemoApplication.class, args);
  }
?
}

------------恢复内容开始------------

spring boot整合Mybatis增删查改

1.选择依赖

SpringWeb,JDBC API, MybatisFramework, MysqlDriver

2.application.yml

将 application.properties改为application.yml(使用更简洁), application.yml文件内容如下:

server:
 port: 8081
?
spring:
 datasource:
   username: root
   password: root
   url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
?
mybatis:
 mapper-locations: classpath:mapping/*Mapper.xml
 type-aliases-package: com.example.entity.demo
 
logging:
 level:
   com:
     example:
       mapper : debug

3.pom.xml

在build中配置resources,来防止我们资源导出失败的问题-->

<build>
   <resources>
       <resource>
           <directory>src/main/resourcesdirectory>
           <includes>
               <include>**/*.properties
               **/*.xmlinclude>
           includes>
           <filtering>truefiltering>
       resource>
       <resource>
           <directory>src/main/javadirectory>
           <includes>
               <include>**/*.properties

               **/*.xmlinclude>
           includes>
           <filtering>truefiltering>
       resource>
   resources>
build>

4.创建包和类

controller包UserController类, entity包User类, mapper包UserMapper(interfac接口), service包UserService类,mapping包UserMapping.xml

技术图片

5.实体类

实体类User类里声明变量,有参构造,Set()、Get()方法, 以及toString()

6.接口UserMapper

package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
?
import java.util.List;
?
/**
* @author x
* @Date 2021/05/24
*/
@Repository
public interface UserMapper {
//   查询全部
   List<User>getAll();
?
// 按id查
   User Sel(@Param("user")User user);
// 增
   int Add(@Param("user")User user);
// 改
   int Update(@Param("user")User user);
// 删
   int Delete(@Param("user")User user);
}

7.UserMapper.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.example.demo.mapper.UserMapper">
?
   <resultMap id="BaseResultMap" type="com.example.demo.entity.User">
       <result column="id" jdbcType="INTEGER" property="id"/>
       <result column="userName" jdbcType="VARCHAR" property="username"/>
       <result column="passWord" jdbcType="VARCHAR" property="password"/>
       <result column="realName" jdbcType="VARCHAR" property="address"/>
   resultMap>

   <select id="getAll" resultType="com.example.demo.entity.User">
       select * from user
   select>

   <select id="Sel" resultType="com.example.demo.entity.User">
       select * from user where 1=1
       <if test="user.id != null">
           AND id = #{user.id}
       if>
   select>

   <insert id="Add" parameterType="com.example.demo.entity.User">
       INSERT INTO user
       <trim prefix="(" suffix=")" suffixOverrides=",">
           <if test="user.username != null">
               username,
           if>
           <if test="user.password != null">
               password,
           if>
           <if test="user.address != null">
               address,
           if>
       trim>
       <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
           <if test="user.username != null">
               #{user.username,jdbcType=VARCHAR},
           if>
           <if test="user.password != null">
               #{user.password,jdbcType=VARCHAR},
           if>
           <if test="user.address != null">
               #{user.address,jdbcType=VARCHAR},
           if>
       trim>
   insert>

   <update id="Update" parameterType="com.example.demo.entity.User">
       UPDATE user
       <set>
           <if test="user.username != null">
               username = #{user.username},
           if>
           <if test="user.password != null">
               password = #{user.password},
           if>
           <if test="user.address != null">
               address = #{user.address},
           if>
       set>
       WHERE
       id=#{user.id}
   update>

   <delete id="Delete"  parameterType="com.example.demo.entity.User">
       DELETE FROM user WHERE id = #{user.id}
   delete>
?
mapper>

8.UserService类

 /*
       Service层介于controller和dao之间作为服务层进行一些逻辑处理,
       这里逻辑太简单所以知识单纯调用dao所以不做注释
    */
package com.example.demo.service;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
?
/**
* @author x
* @Date 2021/05/24
*/
@Service
public class UserService {
   @Autowired
   UserMapper userMapper;
//查询全部
   public List<User>getAll(){
       return userMapper.getAll();
  }
//根据id查询
   public User sel(User user){
       return userMapper.Sel(user);
  }
?
   public  String Add(User user){
       int a = userMapper.Add(user);
       if (a==1){
           return "添加成功";
      }else {
           return "添加失败";
      }
?
  }
?
   public String Update(User user){
       int a = userMapper.Update(user);
       if (a == 1) {
           return "修改成功";
      }else {
           return "修改失败";
      }
  }
?
   public String Delete(User user){
       int a = userMapper.Delete(user);
       if (a == 1) {
           return "删除成功";
      }else {
           return "删除失败";
      }
  }
?
}

9.UserController类

package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @author x
* @Date 2021/05/24
*/
@RestController
@RequestMapping("/test")
public class UserController {
?
   @Autowired
   private UserService userService;
//查询全部
   @RequestMapping(value = "/select", produces = "application/json;charset=UTF-8", method = RequestMethod.GET )
   @ResponseBody
   public String getAll(User user){
       return userService.getAll().toString();
  }
//根据id查询
   @RequestMapping(value = "/selectUserByid", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
   @ResponseBody
   public String GetUser(User user){
       return userService.sel(user).toString();
  }
   
   @RequestMapping(value = "/add", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
   public String Add(User user){
       return userService.Add(user);
  }
?
   @RequestMapping(value = "/update", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
   public String Update(User user){
       return userService.Update(user);
  }
?
   @RequestMapping(value = "/delete", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
   public String Delete(User user){
       return userService.Delete(user);
  }
}

10.DemoApplication测试类

package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author x
* @Date 2021/05/24
*/
@MapperScan("com.example.demo.mapper") //扫描的mapper
@SpringBootApplication
public class DemoApplication {
?
   public static void main(String[] args) {
       SpringApplication.run(DemoApplication.class, args);
  }
?
}

------------恢复内容结束------------

springboot整合mybatis增删查改

标签:rri   request   zone   int   demo   扫描   mysqld   rest   osi   

原文地址:https://www.cnblogs.com/wxdiary/p/14893132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!