springboot+mybatis实现

1、创建user表

 

2、新建项目并引入以下库,也可以通过maven的配置文件导入依赖包(在pom.xml添加)

 

3、分好目录结构

 

 

UserController

 1 package com.Control;
 2 
 3 import com.Control.revice.userparam;
 4 import com.Service.Impl.UserService;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.web.bind.annotation.*;
 7 
 8 /**
 9  * @author: Amim
10  * @year: 2022/9
11  **/
12 @RestController
13 @ResponseBody
14 @RequestMapping("/testBoot")
15 public class UserController {
16 
17     @Autowired
18     private UserService userService;
19 
20     @RequestMapping("getUser/{id}")
21     public String GetUser(@PathVariable int id){
22         return userService.Sel(id).toString();
23     }
24 
25     @PutMapping("/update")
26     public String updateUser(@RequestBody userparam user){
27         return userService.Up(user).toString();
28     }
29 
30     @PutMapping("/insert")
31     public String insertUser(@RequestBody userparam ints){
32         return userService.Int(ints).toString();
33     }
34     @DeleteMapping ("/delete")
35     public String deleteUser(@RequestBody userparam del){
36         return userService.Del(del).toString();
37     }
38 }
userparam
 1 package com.Control.revice;
 2 
 3 import lombok.Data;
 4 
 5 /**
 6  * @author: Amim
 7  * @year: 2022/9
 8  **/
 9 
10 @Data
11 public class userparam {
12      int id;
13      String name;
14      String userName;
15      String passWord;
16      String realName;
17 }
User
 1 package com.Dao;
 2 
 3 import lombok.Data;
 4 
 5 /**
 6  * @author: Amim
 7  * @year: 2022/9
 8  **/
 9 @Data
10 public class User {
11     private Integer id;
12     private String userName;
13     private String passWord;
14     private String realName;
15 
16 //    public Integer getId() {
17 //        return id;
18 //    }
19 //
20 //    public void setId(Integer id) {
21 //        this.id = id;
22 //    }
23 //
24 //    public String getUserName() {
25 //        return userName;
26 //    }
27 //
28 //    public void setUserName(String userName) {
29 //        this.userName = userName;
30 //    }
31 
32 //    public String getPassWord() {
33 //        return passWord;
34 //    }
35 //
36 //    public void setPassWord(String passWord) {
37 //        this.passWord = passWord;
38 //    }
39 //
40 //    public String getRealName() {
41 //        return realName;
42 //    }
43 //
44 //    public void setRealName(String realName) {
45 //        this.realName = realName;
46 //    }
47 //
48 //    @Override
49 //    public String toString() {
50 //        return "User{" +
51 //                "id=" + id +
52 //                ", userName='" + userName + '\'' +
53 //                ", passWord='" + passWord + '\'' +
54 //                ", realName='" + realName + '\'' +
55 //                '}';
56 //    }
57 }
UserMapper
 1 package com.Dao.Mapper;
 2 
 3 import com.Control.revice.userparam;
 4 import com.Dao.User;
 5 
 6 import org.apache.ibatis.annotations.Delete;
 7 import org.apache.ibatis.annotations.Insert;
 8 import org.apache.ibatis.annotations.Update;
 9 import org.springframework.stereotype.Repository;
10 
11 /**
12  * @author: Amim
13  * @year: 2022/9
14  **/
15 @Repository
16 public interface UserMapper {
17     //xmlsql
18     User Sel(int id);
19     int Up(userparam user);
20 //    User Ins(String name,int id,);
21     @Insert( "insert into user(username,password,realname) values(#{userName},#{passWord},#{realName})" )
22     int Int(userparam ints);
23     @Delete( "delete from user where id = #{id}" )
24     int Del(userparam del);
25 }
UserService
 1 package com.Service.Impl;
 2 
 3 import cn.hutool.core.util.StrUtil;
 4 import com.Control.revice.userparam;
 5 import com.Dao.User;
 6 import com.Dao.Mapper.UserMapper;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.stereotype.Service;
 9 
10 /**
11  * @author: Amim
12  * @year: 2022/9
13  **/
14 @Service
15 public class UserService {
16     @Autowired
17     UserMapper userMapper;
18 
19     public User Sel(int id){
20         return userMapper.Sel(id);
21     }
22     public Object Up(userparam user){
23         int num=userMapper.Up(user);
24         return StrUtil.format("更新完毕条数: {}",String.valueOf(num));
25     }
26     public Object Int(userparam ints){
27         int num=userMapper.Int(ints);
28         return StrUtil.format("新增完毕条数: {}",String.valueOf(num));
29     }
30     public Object Del(userparam del){
31         int num=userMapper.Del(del);
32         return StrUtil.format("删除完毕条数: {}",String.valueOf(num));
33     }
34 }
SpringbootApplication
 1 package com;
 2 
 3 import org.mybatis.spring.annotation.MapperScan;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 
 7 @SpringBootApplication()
 8 @MapperScan("com.Dao.mapper")
 9 public class SpringbootApplication {
10     public static void main(String[] args) {
11         SpringApplication.run(SpringbootApplication.class, args);
12     }
13 
14 }
UserMapper.xml  
与UserMapper.java对应关系
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <mapper namespace="com.Dao.Mapper.UserMapper">
 4 
 5     <!--    <resultMap id="BaseResultMap2222" type="com.entity.User">-->
 6     <!--        <result column="id" jdbcType="INTEGER" property="id" />-->
 7     <!--        <result column="userName" jdbcType="VARCHAR" property="userName" />-->
 8     <!--        <result column="passWord" jdbcType="VARCHAR" property="passWord" />-->
 9     <!--        <result column="realName" jdbcType="VARCHAR" property="realName" />-->
10     <!--    </resultMap>-->
11 
12     <select id="Sel" resultType="com.Dao.User">
13         select * from user where id = #{id}
14     </select>
15     <update id="Up" parameterType="com.Control.revice.userparam">
16         update user set userName = #{name} where id = #{id}
17     </update>
18 <!--    <insert id="Ins" parameterType="String">-->
19 <!--        insert into  user (id,username,password,realname) values(#{id},#{userName},#{passWord},#{realName})-->
20 <!--    </insert>>-->
21 <!--    <delete id="del" parameterType="String">-->
22 <!--        insert into  user (id,username,password,realname) values(#{id},#{userName},#{passWord},#{realName})-->
23 <!--    </delete>>-->
24 </mapper>
application.yaml    
注:1、如果不连数据库将useSSL=false 改成 useSSL=true
  2、JDBC连接Mysql5需用com.mysql.jdbc.Driver
    JDBC连接Mysql6需用com.mysql.cj.jdbc.Driver
 1 server:
 2   port: 8002
 3 
 4 spring:
 5   datasource:
 6     username: root
 7     password: 123456
 8     url: jdbc:mysql://localhost:3339/sys?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
 9     driver-class-name: com.mysql.jdbc.Driver
10 
11 mybatis:
12   mapper-locations: classpath:mapper/*Mapper.xml
13   type-aliases-package: TestPro.Dao
14 
15 #showSql
16 logging:
17   level:
18     com:
19       example:
20         mapper : debug
pom.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.7.4</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.test01</groupId>
12     <artifactId>springboot</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>springboot</name>
15     <description>Demo project for Spring Boot</description>
16     <properties>
17         <java.version>1.8</java.version>
18     </properties>
19     <dependencies>
20         <dependency>
21             <groupId>cn.hutool</groupId>
22             <artifactId>hutool-all</artifactId>
23             <version>5.4.1</version>
24         </dependency>
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter-web</artifactId>
28         </dependency>
29         <dependency>
30             <groupId>org.mybatis.spring.boot</groupId>
31             <artifactId>mybatis-spring-boot-starter</artifactId>
32             <version>2.2.2</version>
33         </dependency>
34         <dependency>
35             <groupId>org.projectlombok</groupId>
36             <artifactId>lombok</artifactId>
37             <version>1.16.18</version>
38         </dependency>
39         <!--        <dependency>-->
40         <!--            <groupId>mysql</groupId>-->
41         <!--            <artifactId>mysql-connector-java</artifactId>-->
42         <!--            <scope>runtime</scope>-->
43         <!--        </dependency>-->
44         <dependency>
45             <groupId>org.springframework.boot</groupId>
46             <artifactId>spring-boot-starter-test</artifactId>
47             <scope>test</scope>
48         </dependency>
49 
50         <dependency>
51             <groupId>org.mybatis</groupId>
52             <artifactId>mybatis</artifactId>
53             <version>3.5.2</version>
54         </dependency>
55         <dependency>
56             <groupId>mysql</groupId>
57             <artifactId>mysql-connector-java</artifactId>
58             <scope>runtime</scope>
59         </dependency>
60         <dependency>
61             <groupId>org.springframework.boot</groupId>
62             <artifactId>spring-boot-starter-jdbc</artifactId>
63         </dependency>
64         <dependency>
65             <groupId>mysql</groupId>
66             <artifactId>mysql-connector-java</artifactId>
67             <version>5.1.47</version>
68         </dependency>
69     </dependencies>
70 
71     <build>
72         <plugins>
73             <plugin>
74                 <groupId>org.springframework.boot</groupId>
75                 <artifactId>spring-boot-maven-plugin</artifactId>
76             </plugin>
77         </plugins>
78     </build>
79 
80 </project>

 

posted @ 2022-09-29 14:56  amim  阅读(30)  评论(0编辑  收藏  举报