Example Analysis of springboot-curd based on mybatis Project
Springboot-curd is based on the example analysis of mybatis project, I believe many inexperienced people are helpless about this, so this article summarizes the causes and solutions of the problem, through this article I hope you can solve this problem.
Project Structure:
pom.xml file:
org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE com.liuyang springbootcurd 0.0.1-SNAPSHOT springbootcurd Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.1 junit junit 4.13 test com.github.pagehelper pagehelper-spring-boot-starter 1.2.13 mysql mysql-connector-java 8.0.25 com.mchange c3p0 0.9.5.5 org.apache.commons commons-lang3 org.springframework.boot spring-boot-devtools true org.springframework.data spring-data-commons 2.2.3.RELEASE org.springframework.boot spring-boot-maven-plugin
yml
##Data source configuration spring: ##Hot deployment configuration devtools: restart: enabled: true #Set the directory to restart, add directory files need to restart additional-paths: src/main/java #Fixed the problem of interface reporting 404 after automatic recompilation of project poll-interval: 3000 quiet-period: 1000 datasource: type: com.mchange.v2.c3p0.ComboPooledDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/springboot_mybatis? useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8 username: root password: 123456 ##Configuration of Views freemarker: template-loader-path: classpath*:/views/ charset: UTF-8 content-type: text/html cache: false suffix: .ftl## mybatis configuration mybatis: #storage path of mapping file mapper-locations: classpath*:/mapper/*.xml type-aliases-package: com.liuyang.bean,com.liuyang.vo,com.liuyang.query configuration: ##Underlined to configured map-underscore-to-camel-case: true## pageHelperpagehelper: helper-dialect: mysql##show dao execute sql statement logging: level: com: xxxx: mapper: debug
bean
private Integer userId; private String userName; private String userPwd;
mapper
public interface Usermapper { public User selectuserbyid(Integer id); public User selectUserByName(String userName);// Note the type of return public int insertUser(User user);// Delete data based on id public int deleteUserById(int userId); //Condition query public List selectUserByItem(UserQuery userQuery);}
mapping.xml
select * from t_user where user_id=#{userId} select * from t_user where user_name=#{userName} select * from t_user where user_name= insert into t_user(user_name,user_pwd) values(#{userName},#{userPwd}); delete from t_user where user_id=#{userId} select * from t_user user_name like "%${userName}%"
controller
@RestControllerpublic class Usercontroller { @Resource private Usermapper usermapper; @GetMapping("one/{id}") public User sayUser(@PathVariable Integer id) { System.out.println( id + "