dao层:
package cn.bean;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class BootBean {
private Long id;
private Integer sex;
}
Controller层:
package cn.coll;
import cn.bean.BootBean;
import cn.server.BootServer;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class BootController {
private final BootServer bootServer;
public BootController(BootServer bootServer){
this.bootServer = bootServer;
}
@PostMapping("/boot/info")
public String bootAdd(@RequestBody BootBean bootBean){
int res = bootServer.BootInsert(bootBean);
return "成功";
}
@PutMapping("/boot/info")
public String bootUpdate(@RequestBody BootBean bootBean){
int res = bootServer.BootUpdate(bootBean);
return "修改成功";
}
@GetMapping("/boot/info")
public List<BootBean> bootSelect(BootBean bootBean){
System.out.println(bootBean.getSex());
System.out.println(bootBean.getId());
List<BootBean> bootBeans = bootServer.BootSelect(bootBean);
return bootBeans;
}
}
mapper层:
package cn.mapper;
import cn.bean.BootBean;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
public interface BootMapper extends Mapper<BootBean> {
// 插入
int BootInsert(BootBean u);
// 修改
int BootUpdate(BootBean u);
// 查询所有
List<BootBean> BootSelect(BootBean u);
}
server层:
package cn.server.Impl;
import cn.bean.BootBean;
import cn.mapper.BootMapper;
import cn.server.BootServer;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BootServerImpl implements BootServer {
private final BootMapper bootMapper;
public BootServerImpl(BootMapper bootMapper){
this.bootMapper = bootMapper;
}
@Override
public int BootInsert(BootBean u){
return bootMapper.BootInsert(u);
}
@Override
public int BootUpdate(BootBean u){
return bootMapper.BootUpdate(u);
}
@Override
public List<BootBean> BootSelect(BootBean u){
return bootMapper.BootSelect(u);
}
}
package cn.server;
import cn.bean.BootBean;
import java.util.List;
public interface BootServer{
// 新增
int BootInsert(BootBean u);
// 修改
int BootUpdate(BootBean u);
// 查询所有
List<BootBean> BootSelect(BootBean u);
}
mapper:
<?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="cn.mapper.BootMapper">
<insert id="BootInsert" parameterType="cn.bean.BootBean" >
insert into tb_boot(id, sex) values (#{id},#{sex});
</insert>
<update id="BootUpdate" parameterType="cn.bean.BootBean" >
update tb_boot set sex = #{sex} where id = #{id}
</update>
<select id="BootSelect" resultType="cn.bean.BootBean">
select id, sex from tb_boot
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="sex != null">
and sex = #{sex}
</if>
</where>
order by id asc limit 0,3;
</select>
</mapper>
application.yml:
# 框架设置
spring:
# 读取配置文件
profiles:
active: dev #开发环境
# active: pro #正式环境
# mybatis
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
configuration:
map-underscore-to-camel-case: true
sharding:
jdbc:
datasource:
names: ds0
# 数据源ds0
ds0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://xx.xxx.xx.xx:3306/imageserver?serverTimezone=GMT%2B8&useSSL=false
username: canal
password: canal
config:
sharding:
props:
sql.show: true
tables:
tb_boot: # tb_boot表
key-generator-column-name: id #主键
actual-data-nodes: ds0.tb_boot${0..1} #数据节点,均匀分布
table-strategy: #分表策略
inline: #行表达式
sharding-column: id
algorithm-expression: tb_boot${id % 2} #按模运算分配
# 日志配置
logging:
config: classpath:logging/logback.xml
level:
cn.mapper: debug
pom文件:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.10.RELEASE</version>
<relativePath/>
</parent>
<groupId>org.example</groupId>
<artifactId>sharding</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<sharding.jdbc.version>3.0.0</sharding.jdbc.version>
<mybatis.version>1.3.0</mybatis.version>
<druid.version>1.1.10</druid.version>
<mysql.version>5.1.38</mysql.version>
<lombok.version>1.18.16</lombok.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>${sharding.jdbc.version}</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</project>