今天做的时候遇到了好多问题,第一个问题就是时区问题这个需要把url地址写成这种形式
spring.datasource.url=jdbc:mysql://localhost:3306/myjdbc?serverTimezone=UTC
第二个问题是在pom文件中如果决定用druid的话,需要导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
同时在application.properties配置文件中加下面这句话
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
如果选择用jdbc的话,只需要导入jdbc的依赖就行了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
在做Mapper.xml配置文件时,需要把文件建在和BookMapper.java同一个位置,但是src.mian.java文件夹不能解析.xml文件,为了能是.xml文件起作用,需要在pom文件中导入一个配置
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml
src/main/resources
其实也可以放在resources的包下,只需要在application.properties中加下面这行代码:
代码含义:mapper的位置是classpath下的mapper文件夹里面所有为xml的文件
mybatis.mapper-locations=classpath:mapper/*.xml
springBoot整合mybatis的整体思路是:
1:创建和数据库匹配的实体类,构建getter和setter方法,同时需要toString方法(我第一次忘了toString方法,结果查询出来的结果是每一本书的物理地址);
public class Book {
private Integer Id;
private String username;
private String userpwd;
//省略getter和setter方法
@Override
public String toString() {
return "Book{" +
"Id=" + Id +
", username='" + username + '\'' +
", userpwd='" + userpwd + '\'' +
'}';
}
}
2:创建数据接口访问层,这是一个接口类,在里面定义需要执行的数据库操作方法,所以需要指定这是一个Mapper类,可以在类名上标注@Mapper注解,但是如果有很多个Mapper类,就需要每个都注解,或者在配置类上标注@MapperScan(“com.cz.springboot.mapper”),括号内为mapper类所在的包的地址;
package com.cz.springboot.mapper;
import com.cz.springboot.entity.Book;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface BookMapper {
List<Book> getAllBooks();
int addBook(Book book);
int deleteBookById(Integer Id);
int updateBookById(Book book);
Book getBookById();
}
3:然后创建Mapper类对应的xml文件,定义相应的sql操作,
<?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.cz.springboot.mapper.BookMapper">
<insert id="addBook" parameterType="com.cz.springboot.entity.Book">
insert into book(username,userpwd) values (#{username},#{userpwd})
</insert>
<update id="updateBookById" parameterType="com.cz.springboot.entity.Book">
update book set username=#{username},userpwd=#{userpwd} where Id=#{id}
</update>
<select id="getAllBooks" resultType="com.cz.springboot.entity.Book">
select * from book
</select>
<delete id="daleteBookById" parameterType="Integer">
delete from book where Id=#{id}
</delete>
<select id="getBookById" parameterType="int" resultType="com.cz.springboot.entity.Book">
select * from book where Id=#{id}
</select>
</mapper>
4:创建server类,这个类首先定义BookMapper类的对象,需要用@Autowired方法进行装配,然后把BookMapper类里面的方法实现。
package com.cz.springboot.service;
import com.cz.springboot.entity.Book;
import com.cz.springboot.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookService {
@Autowired
BookMapper bookMapper;
public int addBook(Book book){
return bookMapper.addBook(book);
}
public int updateBookById(Book book){
return bookMapper.updateBookById(book);
}
public int deleteBookById(Integer Id){
return bookMapper.deleteBookById(Id);
}
public List<Book> getAllBooks(){
return bookMapper.getAllBooks();
}
public Book getBookById(Integer id){
return bookMapper.getBookById();
}
}
5:创建controller类,这里需要用@Autowired标签装配BookServer类
package com.cz.springboot.controller;
import com.cz.springboot.entity.Book;
import com.cz.springboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class BookController {
@Autowired
BookService bookService;
@RequestMapping("/book")
public void bookOps(){
List<Book> allBooks = bookService.getAllBooks();
System.out.println("getAllBooks ==> " + allBooks);
}
}
6:最后是运行截图
总结:springBoot整合Mybatis的操作其实还算简单的,掌握了mybatis的操作其实就可以基本实现了。