想整合MyBatis,不单单要加入MyBatis的依赖当然也要加入Mysql和JDBC的依赖这样才能操作数据库。这里使用的工具是IDEA,创建好Springboot项目后先加入Mybatis,Mysql ,JDBC的依赖:
根据自己的需要可以更该版本,如果版本不兼容项目可能运行不起来。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
然后可以配置yml文件
server:
port: 8085
spring:
datasource:
username: root
password: 123
#根据自己的可以更改
url: jdbc:mysql://127.0.0.1:3306/user?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false
#由于我的mysql版本较低所以在url后面加了一些属性才能保证正常运行
driver-class-name: com.mysql.jdbc.Driver
#\u5F00\u542F
mybatis:
#配置MyBatis的配置文件和mapper映射文件路径地址。如果使用注解就不要mapper映射文件
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: mybatis/mapper/*.xml
下面是利用注解进行操作:
@Mapper
public interface DepartmentMapper {
@Select("select * from department where id=#{id}")
public Department getDepartment(Integer id);
@Delete("delete from department where id=#{id}")
public int deleteDepartment(Integer id);
@Options(useGeneratedKeys = true, keyProperty = "id")
@Insert("insert into department(name) values(#{name})")
public int insertDepartment(Department department);
@Update("update department set name=#{name} where id=#{id}")
public int updateDepartment(Department department);
}
@RestController//如果不想在页面输出return的内容就需要换成@Controller注解。刚学的时候我踩过坑就是在跳转页面的时候显示的一直都是return的内容
public class DepartController {
@Autowired
DepartmentMapper mapper;
@Autowired
EmpMapper empMapper;
@GetMapping("/dept/{id}")
public Department getDepart(@PathVariable("id") Integer id){
return mapper.getDepartment(id);
}
@GetMapping("/dept")
public Department insert(Department department){
mapper.insertDepartment(department);
return department;
}
@GetMapping("/emp/{id}")
public Employee getEmp(@PathVariable("id") Integer id){
return empMapper.getEmp(id);
}
@GetMapping("/insert")
public Employee insertEmp(Employee emp){
empMapper.insertEmp(emp);
return emp;
}
}
下面使用mapper的映射文件操作:
先定义一个接口类
public interface EmpMapper {
public Employee getEmp(Integer id);
public void insertEmp(Employee employee);
}
然后在创建一个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.example.mybatisdemo.mapper.EmpMapper">
<!--id值就是接口类中对应的方法名,resultType是查询结果的返回类型-->
<select id="getEmp" resultType="com.example.mybatisdemo.bean.Employee">
select * from employee where id=#{id}
</select>
<insert id="insertEmp">
insert into employee(name,addr) values(#{name},#{addr})
</insert>
</mapper>
这个是MyBatis的配置文件,MyBatis的一些配置可以在此配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!--开启驼峰命名-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
下面是我的文件结构