创建springboot
用到的工具
首先创建文件
创建如下文件夹
有一些删掉
代码部分
UserController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 package com.example.springbootdemo.controller;import com.example.springbootdemo.mapper.UserMapper;import com.example.springbootdemo.pojo.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/queryUserList") public List<User> queryUserList () { List<User> userList = userMapper.queryUserList(); for (User user:userList){ System.out.println(user); } return userList; } @GetMapping(value = "test") public String test () { return "测试接口!!!!" ; } @GetMapping("/addUser") public String addUser () { userMapper.addUser(new User(3 ,"小王" ,"345" )); return "addUser-ok" ; } @GetMapping("/updateUser") public String updateUser () { userMapper.updateUser(new User(3 ,"小张" ,"567" )); return "updateUser-ok" ; } @GetMapping("/deleteUser") public String deleteUser () { userMapper.deleteUser(3 ); return "updateUser-ok" ; } }
UserMapper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package com.example.springbootdemo.mapper;import com.example.springbootdemo.pojo.User;import org.apache.ibatis.annotations.Mapper;import org.springframework.stereotype.Repository;import java.util.List;@Mapper @Repository public interface UserMapper { List<User> queryUserList () ; User queryUserById (int id) ; int addUser (User user) ; int updateUser (User user) ; int deleteUser (int id) ; }
User
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.example.springbootdemo.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data @NoArgsConstructor @AllArgsConstructor public class User { private int id; private String name; private String pwd; }
SperingbootdemoApplication
不用改
UserMapper.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <?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.springbootdemo.mapper.UserMapper" > <select id="queryUserList" resultType="com.example.springbootdemo.pojo.User" > select * from user </select> <select id="queryUserById" resultType="com.example.springbootdemo.pojo.User" > select * from user where id = #{id} </select> <insert id="addUser" parameterType="com.example.springbootdemo.pojo.User" > insert into user (id,name,pwd) values (#{id},#{name},#{pwd}) </insert> <update id ="updateUser" parameterType="java.lang.Integer" > update user set name = #{name},pwd={pwd} where id = #{id} </update> <delete id="deleteUser" parameterType="java.lang.Integer" > delete from user where id = #{id} </delete> </mapper>
application.properties
1 2 3 4 5 6 7 8 spring.datasource.username=root //数据库账号 spring.datasource.password=1234 //数据库密码 spring.datasource.url=jdbc:mysql://localhost:3306/springbootdemodb?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver server.port=8082 //你选择的端口有时候被占用改一下就行 #??mybatis mybatis.type-aliases-package=com.example.springbootdemo.pojo mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
注意spring.datasource.url=jdbc:mysql://localhost:3306/springbootdemodb(这里是你的数据库名称)?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.zya</groupId> <artifactId>springboot-mybais_01</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-mybais_01</name> <description>springboot-mybais_01</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
最后贴一下数据库
运行
运行SpringbootdemoApplication
在网页打开
localhost://8082/test
测试是否连接成功
localhost://8082/addUser
添加用户,添加的用户为你在UserController中设定的
改删类似,不在赘述。