使用SpringBoot做的一个CRUD的手脚架,并且提供了数据库密码的加密功能。
项目准备
进入“https://start.spring.io/”构建SpringBoot项目;
选择“Web”、“MySQL”、“Mybatis”模块;
导入项目到idea;
配置数据库
新建数据库;
1 2 create database test; use test;
新建表
1 2 3 4 5 6 7 8 9 10 11 DROP TABLE `account`;CREATE TABLE `account` ( `id` int (11 ) NOT NULL AUTO_INCREMENT, `name` varchar (20 ) NOT NULL , `money` double DEFAULT NULL , PRIMARY KEY (`id`) ) ENGINE= InnoDB AUTO_INCREMENT= 4 DEFAULT CHARSET= utf8; INSERT INTO `account` VALUES ('1' , 'aaa' , '1000' );INSERT INTO `account` VALUES ('2' , 'bbb' , '1000' );INSERT INTO `account` VALUES ('3' , 'ccc' , '1000' );
配置数据库连接 application.properties文件
1 2 3 4 5 6 7 8 9 10 11 spring.datasource.url =jdbc:mysql://localhost:3306/test?serverTimezone=UTC spring.datasource.username =ENC(YMoG7LZvcKq3DbW3dc9Ltg==) spring.datasource.password =ENC(RgqVyviuib6ScIcgcQr9SA==) spring.datasource.driver-class-name =com.mysql.jdbc.Driver jasypt.encryptor.password =erp debug =true logging.level.top.dengwq.springboot_crud.mapper =debug
Entity 1 2 3 4 5 6 7 8 @Data @ToString @EqualsAndHashCode public class Account { private int id ; private String name ; private double money; }
Mapper 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @Mapper public interface AccountMapper { @Insert("insert into account(name, money) values(#{name}, #{money})") int add (@Param("name") String name, @Param("money") double money) ; @Update("update account set name = #{name}, money = #{money} where id = #{id}") int update (@Param("name") String name, @Param("money") double money, @Param("id") int id) ; @Delete("delete from account where id = #{id}") int delete (int id) ; @Select("select id, name as name, money as money from account where id = #{id}") Account findAccount (@Param("id") int id) ; @Select("select id, name as name, money as money from account") List<Account> findAccountList () ; }
Service 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 @Service public class AccountService { @Autowired private AccountMapper accountMapper; public int add (String name, double money) { return accountMapper.add(name, money); } public int update (String name, double money, int id) { return accountMapper.update(name, money, id); } public int delete (int id) { return accountMapper.delete(id); } public Account findAccount (int id) { return accountMapper.findAccount(id); } public List<Account> findAccountList () { return accountMapper.findAccountList(); } }
Controller 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 @RestController @RequestMapping("/account") public class AccountController { @Autowired AccountService accountService; @RequestMapping(value = "/list", method = RequestMethod.GET) public List<Account> getAccounts () { return accountService.findAccountList(); } @RequestMapping(value = "/{id}", method = RequestMethod.GET) public Account getAccountById (@PathVariable("id") int id) { return accountService.findAccount(id); } @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public String updateAccount (@PathVariable("id") int id, @RequestParam(value = "name", required = true) String name, @RequestParam(value = "money", required = true) double money) { int t= accountService.update(name,money,id); if (t==1 ) { return "success" ; }else { return "fail" ; } } @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) public String delete (@PathVariable(value = "id") int id) { int t= accountService.delete(id); if (t==1 ) { return "success" ; }else { return "fail" ; } } @RequestMapping(value = "", method = RequestMethod.POST) public String postAccount (@RequestParam(value = "name") String name, @RequestParam(value = "money") double money) { int t= accountService.add(name,money); if (t==1 ) { return "success" ; }else { return "fail" ; } } }
加密组件
依赖
1 2 3 4 5 <dependency > <groupId > com.github.ulisesbocchio</groupId > <artifactId > jasypt-spring-boot-starter</artifactId > <version > 2.1.1</version > </dependency >
添加盐值
1 2 jasypt.encryptor.password =erp
明文加密
1 2 3 4 5 6 7 8 9 10 11 12 @Test public void getPass () { String name = encryptor.encrypt("root" ); String password = encryptor.encrypt("123456" ); System.out.println("Name加密后明文: " + name); System.out.println("Password加密后明文: " + password); Assert.assertTrue(name.length() > 0 ); Assert.assertTrue(password.length() > 0 ); }
使用(application.properties)
1 2 3 # ENC() 是数据库加密函数 spring.datasource.username=ENC(YMoG7LZvcKq3DbW3dc9Ltg==) spring.datasource.password=ENC(RgqVyviuib6ScIcgcQr9SA==)