集成Mybatis
mybatis支持注解和xml两种方式,现在通过这两种方式来将mybatis集成进springboot中
注解
配置数据源
spring.datasource.url=jdbc:mysql://localhost:3306/scmdb
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.configuration.map-underscore-to-camel-case=true
建立实体
package springbootmybatis.mybatisdemo01.entity;
import org.springframework.stereotype.Component;
@Component
public class User {
public int id;
public String username;
public String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
建立Mapper
package springbootmybatis.mybatisdemo01.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import springbootmybatis.mybatisdemo01.entity.User;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("select * from user where id=#{id}")
public User findUserById(int id);
@Select("select * from user")
public List<User> findUser();
}
建立控制器
由于用于演示,我没有使用service服务,直接使用mapper进行调用
package springbootmybatis.mybatisdemo01.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springbootmybatis.mybatisdemo01.entity.User;
import springbootmybatis.mybatisdemo01.mapper.UserMapper;
import java.util.List;
@RestController
public class UserController {
@Autowired
public UserMapper userMapper;
@RequestMapping(value = "/getall",method = RequestMethod.GET)
public List<User> getAll(){
return userMapper.findUser();
}
}
ps:在idea下,这里的usermapper会标红,这里其实是Idea的问题,没有识别到我们的mapper实现方式,大家这里不用管,直接编译就行,不会报错。
测试
xml方式
创建account实体
package springbootmybatis.mybatisdemo01.entity;
public class account {
public int id;
public int uid;
public String accountName;
public double money;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getAccountname() {
return accountName;
}
public void setAccountname(String accountname) {
this.accountName = accountname;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
建立accountmapper
package springbootmybatis.mybatisdemo01.mapper;
import org.apache.ibatis.annotations.Mapper;
import springbootmybatis.mybatisdemo01.entity.account;
import java.util.List;
@Mapper
public interface accountMapper {
public List<account> findaccountall();
}
创建mapper 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="springbootmybatis.mybatisdemo01.mapper.accountMapper">
<select id="findaccountall" resultType="account">
select * from account;
</select>
</mapper>
由于xml文件所在的包名与mapper类所在的包名不一致,需要进行location映射
mybatis.mapper-locations=classpath:mapper/*.xml
由于使用了别名,也需要进行别名包的处理
mybatis.type-aliases-package=springbootmybatis.mybatisdemo01.entity
建立控制器
package springbootmybatis.mybatisdemo01.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import springbootmybatis.mybatisdemo01.entity.account;
import springbootmybatis.mybatisdemo01.mapper.accountMapper;
import java.util.List;
@RestController
public class accountController {
@Autowired
public accountMapper accountMapper;
@GetMapping("/account")
public List<account> getAll(){
return accountMapper.findaccountall();
}
}
测试
总结
在测试中发现几个问题:
1.mysql版本太高,导致启动不起来
通过修改myql版本号解决
2.没有配置account.xml的映射路径,导致查询报错。
通过配置mybatis.mapper-locations解决