基于【springboot篇】六. springboot中的事务控制
springboot中的crud
项目准备
- 创建项目day64-springboot-05-crud
- 项目配置参考上一章
- 新建数据库day64-springboot-crud
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(8) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, `pwd` varchar(20) DEFAULT NULL, `age` int(3) DEFAULT NULL, `money` double(8,2) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; INSERT INTO `user` VALUES ('1', 'wpj', '123456', '23', '10000.00'); INSERT INTO `user` VALUES ('2', 'czh', '123456', '22', '10000.00');
- 创建实体类User
package com.wpj.bean; import com.baomidou.mybatisplus.annotations.TableId; import com.baomidou.mybatisplus.annotations.TableName; import com.baomidou.mybatisplus.enums.IdType; import lombok.Data; @Data @TableName("user") public class User { @TableId(type = IdType.AUTO) private Integer id; private String name; private String pwd; private Integer age; private Double money; }
- 创建mapper接口和service及其impl
package com.wpj.mapper; import com.baomidou.mybatisplus.mapper.BaseMapper; import com.wpj.bean.User; public interface IUserMapper extends BaseMapper<User> { }
package com.wpj.service; import com.wpj.baseservice.IBaseService; import com.wpj.bean.User; public interface IUserService extends IBaseService<User> { }
package com.wpj.service.impl; import com.baomidou.mybatisplus.mapper.BaseMapper; import com.baomidou.mybatisplus.plugins.Page; import com.wpj.baseservice.impl.BaseServiceImpl; import com.wpj.bean.User; import com.wpj.mapper.IUserMapper; import com.wpj.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService { @Autowired private IUserMapper iUserMapper; @Override protected BaseMapper<User> getBaseMapper() { return iUserMapper; } }
- 创建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="com.wpj.mapper.IUserMapper"> </mapper>
- application.properties修改为application.yml
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/day64-springboot-crud username: root password: 123456 mybatis-plus: type-aliases-package: com.wpj.bean mapper-locations: classpath:mapper/*.xml
- 开启包扫描
package com.wpj.day64springboot05crud; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication(scanBasePackages = "com.wpj") @MapperScan(basePackages = "com.wpj.mapper") public class Day64Springboot05CrudApplication { public static void main(String[] args) { SpringApplication.run(Day64Springboot05CrudApplication.class, args); } }
- 测试
10.测试环境完成。。。。。。。开始做增删改查。