# springdome
**Repository Path**: 1111pp/springdome
## Basic Information
- **Project Name**: springdome
- **Description**: 个人springdome 测试
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 3
- **Created**: 2020-07-17
- **Last Updated**: 2021-11-03
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# springdome
#### 介绍
个人springdome 测试
#### 软件架构
mybatis
#### 1、导入表结构
```sql
create table test.user
(
id bigint not null AUTO_INCREMENT comment '主键' primary key,
age int null comment '年龄',
password varchar(32) null comment '密码',
sex int null comment '性别',
username varchar(32) null comment '用户名'
);
```
#### 2、pom包
```xml
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.1
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-test
test
```
#### 3、yml配置
```yml
server:
port: 8081
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
password: adminroot
username: root
url: jdbc:mysql://localhost:3306/vue_authentication?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
mybatis:
mapper-locations: classpath:mapper/*.xml
```
#### 4、编写对应的实体类
>路径 com.dy.mybatis.entity.User
```java
package com.dy.mybatis.entity;
import lombok.Data;
/**
* @author huangdeyao
*/
@Data
public class User {
private Long id;
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 年龄
*/
private Integer age;
/**
* 性别 1=男 2=女 其他=保密
*/
private Integer sex;
}
```
#### 5、编写mapper接口
>路径 com.dy.mybatis.repository.UserMapper
```java
/**
* @author huangdeyao
*/
@Service
@Mapper
public interface UserMapper {
/**
* 新增用户
*
* @param user
* @return
*/
int save(User user);
/**
* 更新用户信息
*
* @param user
* @return
*/
int update(User user);
/**
* 根据id删除
*
* @param id
* @return
*/
int deleteById(int id);
/**
* 根据id查询
*
* @param id
* @return
*/
User selectById(int id);
/**
* 查询所有用户信息
*
* @return
*/
List selectAll();
}
```
#### 6、UserMapper.xml
>路径 resources/mapper/UserMapper.xml
```xml
delete from user where id=#{id}
insert into user
id,
username,
password,
sex,
age,
#{id,jdbcType=BIGINT},
#{username,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{sex,jdbcType=INTEGER},
#{age,jdbcType=INTEGER},
update user
username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
sex = #{sex,jdbcType=INTEGER},
sex = #{age,jdbcType=INTEGER},
where id = #{id,jdbcType=BIGINT}
```
#### 7、controller 测试
```java
package com.dy.mybatis.controllers;
import com.dy.mybatis.entity.User;
import com.dy.mybatis.repository.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author huangdeyao
* @date 2019/7/8 16:29
*/
@RestController
public class MybatisController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/save")
public int save() {
User user = new User();
user.setUsername("zzzz");
user.setPassword("bbbb");
user.setSex(1);
user.setAge(18);
// 返回插入的记录数 ,期望是1条 如果实际不是一条则抛出异常
return userMapper.save(user);
}
@RequestMapping("/update")
public int update() {
User user = new User();
user.setId(1L);
user.setPassword("newpassword");
// 返回更新的记录数 ,期望是1条 如果实际不是一条则抛出异常
return userMapper.update(user);
}
@GetMapping("/find")
public User selectById() {
return userMapper.selectById(1);
}
@GetMapping("/delete")
public int deleteById() {
return userMapper.deleteById(1);
}
@GetMapping("/all")
public List all() {
return userMapper.selectAll();
}
}
```
#### 注意版本
> 1、版本有出入也会有问题;pom里面的版本 boot 2.1.6, mybatis 2.0.1
> 2、 注意parameterType中的路径,yml中的配置路径
[码云](https://gitee.com/dy.huang/springdome)
reference
---
[【SpringBoot2.0系列05】SpringBoot之整合Mybatis](https://www.jianshu.com/p/c44dc639cb93)
[基于 SpringBoot2.0+优雅整合 SpringBoot+Mybatis](https://segmentfault.com/a/1190000017211657)