开发环境:SpringBoot1.5.2、MyBatis3.5、jdk1.8、MySQL5.7.20
一、创建Maven项目参考:SpringBoot第 1 讲:HelloWorld_秦毅翔的专栏-CSDN博客
二、修改pom.xml三、配置MyBatis 3.1、jdbc.properties4.0.0 org.springframework.boot spring-boot-starter-parent1.5.2.RELEASE org.personal.qin.demos mybatis_demo1.0.0-SNAPSHOT jar UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-weborg.springframework spring-webmvcmysql mysql-connector-javaorg.mybatis.spring.boot mybatis-spring-boot-starter1.3.2 org.mybatis mybatis-spring1.3.1 org.mybatis mybatis3.5.0 com.jolbox bonecp-spring0.8.0.RELEASE ${project.artifactId} org.apache.maven.plugins maven-resources-pluginUTF-8 org.apache.maven.plugins maven-compiler-plugin1.8 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin
#jdbc.driver=com.mysql.cj.jdbc.Driver #5.8 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://10.211.55.4:3306/t_mybatis?characterEncoding=utf-8&useSSL=false jdbc.username=root jdbc.password=Aa123123. jdbc.period_in_minutes=60 jdbc.max_age_in_minutes=30 jdbc.max_connections_per_partition=100 jdbc.min_connections_per_partition=53.2、po和mapper路径配置Cast.java
package demo.mybatis.config;
public class Cast {
//配置mybatis-config.xml文件的路径(配置MyBatis特有的增强功能)
public static final String MYBATIS_ConFIG = "classpath:mybatis-config.xml";
//配置PO实体的包路径
public static final String PO_PACKAGE = "demo.mybatis.entity";
//配置DAO(Mapper)接口的包路径
public static final String MAPPER_PACKAGE = "demo.mybatis.mapper";
//配置DAO(Mapper)xml文件的扫描路径
public static final String MAPPER_SCANNER = "classpath:mappers/*.xml";
}
四、自定义Mapper
4.1、UserMapper接口
package demo.mybatis.mapper;
import java.util.List;
import java.util.Map;
import demo.mybatis.entity.User;
public interface UserMapper {
public List selectAll();
public User selectByAuth(Map params);
public int insertUser(User user);
public int deleteUserById(int id);
public int updateUser(User user);
}
4.2、UserMapper.xml
前提是要在Cast正确配置po和mapper接口的包路径
五、启动BootApplication
package demo.mybatis;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import demo.mybatis.entity.User;
import demo.mybatis.mapper.UserMapper;
@SpringBootApplication
@RestController
@RequestMapping("user")
public class MyBatisBootApplication {
@Autowired
private UserMapper mapper;
@GetMapping
public List test1(){
return mapper.selectAll();
}
@GetMapping("/{name}/{password}")
public User test2(@PathVariable String name, @PathVariable String password) {
Map params = new HashMap();
params.put("name", name);
params.put("password", password);
return mapper.selectByAuth(params);
}
@PostMapping("/{name}/{password}")
public String test3(@PathVariable String name, @PathVariable String password) {
if(mapper.insertUser(new User(0, name, password)) > 0) {
return "添加用户成功";
} else {
return "添加用户失败";
}
}
@DeleteMapping("/{id}")
public String test3(@PathVariable int id) {
if(mapper.deleteUserById(id) > 0) {
return "删除用户成功";
} else {
return "删除用户失败";
}
}
@PutMapping("/{id}/{name}/{password}")
public String test4(@PathVariable int id, @PathVariable String name, @PathVariable String password) {
if(mapper.updateUser(new User(id, name, password)) > 0) {
return "修改用户成功";
} else {
return "修改用户失败";
}
}
public static void main(String[] args) {
SpringApplication.run(MyBatisBootApplication.class, args);
}
}
六、测试
数据库
浏览器请求:
http://127.0.0.1:8080/user
七、源代码
https://download.csdn.net/download/qzc70919700/30526342



