目录
一 SpringBoot整合JdbcTemplate
1、引入JdbcTemplate依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> 2、编写配置文件 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://ip地址:3306/library?useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: 用户名 password: 密码 type: com.alibaba.druid.pool.DruidDataSource
3、示例代码
-
-
public class AdminService {
-
-
private JdbcTemplate jdbcTemplate;
-
public boolean insertAdmin(String username, String password) {
-
int update = jdbcTemplate.update("INSERT INTO admin VALUES(1,?,?)", username, password);
-
return update > 0 ? true : false;
-
}
二 SpringBoot整合Mybatis
1、引入Mybatis依赖
-
<dependency>
-
<groupId>org.mybatis.spring.boot</groupId>
-
<artifactId>mybatis-spring-boot-starter</artifactId>
-
<version>2.1.3</version>
-
</dependency>
2、编写配置文件
-
spring:
-
jpa:
-
show-sql: true
-
database: mysql
-
hibernate:
-
ddl-auto: update
-
open-in-view: false
3、示例代码
-
-
public interface AdminMapper {
-
-
List<Admin> findAll();
-
}
三 SpringBoot整合Jpa
1、引入Jpa依赖
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-data-jpa</artifactId>
-
</dependency>
2、示例代码
实体类 @Data @Entity(name ="t_book") public class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(name = "book_name",nullable = false) private String name; private String author; private Float price; @Transient private String description; } dao层 public interface BookDao extends JpaRepository<Book, Integer> { Book getBookById(Integer id); @Transactional @Modifying @Query("UPDATE t_book set book_name='发展之道' WHERE id =:id") void update(Integer id); } service层 public interface BookService { Book getBookById(Integer id); List<Book> findAll(); void save(Book book); void deleteById(Integer id); void update(Integer id); } @Service public class BookServiceImpl implements BookService { @Autowired BookDao bookDao; @Override public Book getBookById(Integer id) { return bookDao.getBookById(id); } @Override public List<Book> findAll() { return bookDao.findAll(); } @Override public void save(Book book) { bookDao.save(book); } @Override public void deleteById(Integer id) { bookDao.deleteById(id); } @Override public void update(Integer id) { bookDao.update(id); } } Controller层 @RestController @RequestMapping("/book") public class BookController { @Autowired BookService bookService; @RequestMapping("/getBookById/{id}") public Book getBookById(@PathVariable Integer id) { return bookService.getBookById(id); } @RequestMapping("/list") public List<Book> findAll() { return bookService.findAll(); } @RequestMapping("/save") public void saveBook() { Book book = new Book(); book.setName("银行理论"); book.setAuthor("浙江农信"); book.setPrice((float) 20); bookService.save(book); } @RequestMapping("/delete/{id}") public void deleteBook(@PathVariable Integer id) { bookService.deleteById(id); } @RequestMapping("/update/{id}") public void updateBook(@PathVariable Integer id) { bookService.update(id); } }