- 引入依赖
在pom.xml 引入ORM框架(Mybaits-Starter)和数据库驱动(MySQL-Conn)的依赖
org.springframework.boot
spring-boot-starter-parent
1.5.6.RELEASE
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
2添加数据源
spring.datasource.url=jdbc:mysql://127.0.0.1/springboot2?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
spring.datasource.username=root
spring.datasource.password=514730
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
spring.thymeleaf.cache=false
#中文
spring.http.encoding.charset=UTF-8
spring.http.encoding.force=true
spring.http.encoding.enabled=true
- 编写数据层代码
//entity类如下
public class Book {
private long id;
private String name;
private String writer;
private String introduction;
//省略 set get toString
//数据层代码如下:
package com.neo.repository;
import com.neo.entity.Book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface BookMapper {
// @Select("select * from book where 1=1")
@Select("select * from book where 1=1 ")
List Book();
@Select("select * from book where name like '%${name}%'")
List findBookByName(@Param("name") String name);
}
4服务处
package com.neo.service.impl;
import com.neo.entity.Book;
import com.neo.repository.BookMapper;
import com.neo.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
@SuppressWarnings("all")
@Autowired
private BookMapper mapper;
@Override
public List Book() {
return mapper.Book();
}
@Override
public List findBookByName(@Param("name")String name) {
return mapper.findBookByName(name);
}
}
----------------
package com.neo.service;
import com.neo.entity.User;
import java.util.List;
public interface UserService {
public List getUserList();
public User findUserById(long id);
public void save(User user);
public void edit(User user);
public void delete(long id);
}
- 添加数据库记录
在Navicat 连接本地数据库,随便打开查询窗口,复制下面这段脚本,点击执行即可。
CREATE TABLE book
(
id INT AUTO_INCREMENT
PRIMARY KEY,
name VARCHAR(255) NULL,
writer VARCHAR(255) NULL,
introduction VARCHAR(255) NULL
)
ENGINE = InnoDB;
6 controller
package com.neo.web;
import com.neo.entity.Book;
import com.neo.entity.People;
import com.neo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.*;
@RestController
public class Controller {
@Autowired
BookService service;
@GetMapping("book")
public List getBook(){
return service.Book();
}
@GetMapping("book/{name}")
public List getBook(@PathVariable("name") String name){
return service.findBookByName(name);
}
}
访问
http://localhost:8080/book/
http://localhost:8080/book/三
https://www.jianshu.com/p/fa89b59ade40
Mybatis like查询的写法
Mybatis like查询官方文档没有明确的例子可循,网上搜索了很多,都不正确。
Mybatis 3.2.6
经过尝试,给出三种可靠可用的写法:
select * from person where name like "%"#{name}"%"
select * from person where name like '%'||#{name}||'%'
select * from person where name like '%${name}%'
三种写法对比后,第一种属于预编译SQL,后两种都不是,因此推荐使用第一种写法。
http://blog.51cto.com/lavasoft/1386870
mybaits错误解决:There is no getter for property named ... in 'class java.lang.String'
也可以在mapper的接口中,给这个方法的参数加上@Param(value=“parentId”),这样就能在.xml中使用#{parentId} 了。
如:public List
https://blog.csdn.net/ybcljay/article/details/80831607