一、基础环境
- 项目环境
- JDK1.8
- gradle 4.X
- 新增微服务主项目后再新增微服务项目,在新增的微服务项目中lib报下添加以下包,并且添加到运行环境中
HikariCP-2.6.3.jar
jasypt-1.9.2.jar
jasypt-spring-boot-1.18.jar
jasypt-spring-boot-starter-1.18.jar
jsqlparser-0.9.5.jar
mybatis-3.4.4.jar
mybatis-plus-2.1.9.jar
mybatis-plus-core-2.1.9.jar
mybatis-plus-generate-2.1.9.jar
mybatis-plus-support-2.1.9.jar
mybatis-spring-1.3.1.jar
mybatis-spring-boot-autoconfigure-1.3.0.jar
mybatis-spring-boot-starter-1.3.0.jar
mybatisplus-spring-boot-starter-1.0.5.jar
hutool-all-4.0.9.jar
- 在application.properties配置加密盐(即下方ENC(XXXXXXXXX))
jasypt.encryptor.password=e!Jd&ljyJ^e4I5oU
- 在application.properties配置数据源
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
#spring.datasource.username=dept
#spring.datasource.password=novell
#加密敏感信息
spring.datasource.username=ENC(ukdPxk2YeSrilKZFvsNr2g==)
spring.datasource.password=ENC(nK0oIOT1Mx6Kn27UhntqBg==)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.pool-name=DataBaseHikari
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=15
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1
- 添加mybatis和其插件支持
# mybatis-plus
# 映射mybatis的xml文件
mybatis-plus.mapper-locations = classpath:mapper/*.xml
# 映射实体类包
mybatis-plus.typeAliasesPackage = com.gdtnx.cloud.**.domain
至此,该项目已支持Mybatis
二、样例说明
- 单表CURD,使用Mybastis-plus插件
- domain 层
@TableName("tb_sys_user")//标注数据库表名 public class User implements Serializable { private static final long serialVersionUID = -5858254991390168074L; /** 用户ID */ //必须设置主键 @TableId(value = "id", type = IdType.ID_WORKER_STR) private String id; /** 用户名 */ private String name; //省略get、set; }
- mapper 层
public interface UserMapper extends BaseMapper<User> { //自定义mybatis查询 List selectUserList(Pagination page, @Param("params") Map<String, Object> map); }
- service 层
接口
public interface IUserService extends IService<User> { }
实现
@Service public class UserService extends ServiceImpl<UserMapper, User> implements IUserService { }
- controller 层
@RestController @RequestMapping("user") public class UserController { //注入service @Autowired private IUserService service; /** * add:(新增). <br/> */ @PostMapping(path = GlobalRoute.ADD) public ItemResponse add(@RequestBody ItemRequest item) { try { User model = (User) item.toBean(User.class); this.service.insert(model); return new ItemResponse(model); } catch (Exception e) { return new ItemResponse(HttpItemStatus.DATABASE_ERROR); } } /** * edit:(编辑). <br/> */ @PostMapping(path = GlobalRoute.EDIT) public ItemResponse edit(@RequestBody ItemRequest item) { try { User model = (User) item.toBean(User.class); this.service.updateById(model); return new ItemResponse(this.service.selectById(model.getId())); } catch (Exception e) { return new ItemResponse(HttpItemStatus.DATABASE_ERROR); } } /** * delete:(删除). <br/> */ @PostMapping(path = GlobalRoute.DEL) public ItemResponse delete(@RequestBody ItemRequest item) { try { User model = (User) item.toBean(User.class); this.service.deleteById(model.getId()); return new ItemResponse(model.getId()); } catch (Exception e) { return new ItemResponse(HttpItemStatus.DATABASE_ERROR); } } /** * get:(根据ID查询按钮组信息). <br/> */ @PostMapping(path = GlobalRoute.GET) public ItemResponse get(@RequestBody ItemRequest item) { try { User model = (User) item.toBean(User.class); return new ItemResponse(this.service.selectById(model.getId())); } catch (Exception e) { return new ItemResponse(HttpItemStatus.DATABASE_ERROR); } } /** * list:(根据属性查询). <br/> */ @PostMapping(path = GlobalRoute.LIST) public ItemResponse list(@RequestBody ItemRequest item) { User model = (User) item.toBean(User.class); EntityWrapper<User> wrapper = new EntityWrapper<>(model); return new ItemResponse(this.service.selectList(wrapper)); } /** * page:(分页查询). <br/> */ @PostMapping(path = GlobalRoute.PAGE) public ItemResponse page(@RequestParam Map<String, Object> params, @RequestBody ItemRequest item) { User model = (User) item.toBean(User.class); Page<User> page = this.service.selectPage(new Condition<>(params), new EntityWrapper<>(model)); return new ItemResponse(page); } /** * list1:(自定义xml查询,继承mybatis-plus的基类). <br/> */ @SuppressWarnings({ "rawtypes" ,"unchecked"}) @PostMapping(path = GlobalRoute.LIST+"1") public ItemResponse list1(@RequestParam Map<String, Object> params, @RequestBody ItemRequest item) { Map map = (Map) item.toBean(Map.class); Page page = this.service.selectUserPage(new Condition<>(params), map); return new ItemResponse(page); } }
- mybatis 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.gdtnx.cloud.demo.mapper.UserMapper"> <select id="selectUserList" resultType="java.util.Map" parameterType="java.util.Map"><!--使用Map传参--> SELECT * FROM tb_sys_user <if test="params.state!=null"> WHERE state=#{params.state} </if> </select> </mapper>
三、数据传输加密
@Encrypt加密 @Decrypt解密 @EnableEncrypt 启用加密功能,注意点:
- 返回类型强制使用
ItemResponse
类 - 请求方法强制使用
POST
提交 - 请求数据需用
ItemRequest
接收,并且使用@RequestBody
注解
@Decrypt //将请求对象的加密数据进行解密(@Decrypt(false)可强制使用false不解密)
@Encrypt //将返回对象的数据加密(@Encrypt(false)可强制使用false不加密)
@PostMapping(path = GlobalRoute.LIST)
public ItemResponse list(@RequestBody ItemRequest item) {
User model = (User) item.toBean(User.class);
EntityWrapper<User> wrapper = new EntityWrapper<>(model);
return new ItemResponse(this.service.selectList(wrapper));
}
启动类上
@MapperScan("com.gdtnx.cloud.**.mapper") //扫描mybatis的接口类
@EnableEncrypt //启用加密功能
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
// 注册分页插件
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor page = new PaginationInterceptor();
return page;
}
}
网友评论