1.需要导入相关的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2.数据源配置
#数据源配置
spring.datasource.url=jdbc:mysql://localhost:3308/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
3.建表
CREATE TABLE `book` (
`book_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`book_name` varchar(32) DEFAULT NULL COMMENT '书名',
`book_descript` varchar(32) DEFAULT NULL COMMENT '书本描述',
`book_price` float(11) DEFAULT NULL COMMENT '书本价格',
PRIMARY KEY (`book_id`)
) ENGINE=InnoDB AUTO_INCREMENT=102 DEFAULT CHARSET=utf8;
4.创建实体类
public class Book {
private Long bookId;
private String bookName;
private String bookDescript;
private float bookPrice;
public Book(String bookName, String bookDescript, float bookPrice) {
this.bookName = bookName;
this.bookDescript = bookDescript;
this.bookPrice = bookPrice;
}
public Long getBookId() {
return bookId;
}
public String getBookName() {
return bookName;
}
public String getBookDescript() {
return bookDescript;
}
public float getBookPrice() {
return bookPrice;
}
}
5.封装Repository
public interface BookRepository {
int save(Book book);
int update(Book book);
int delete(Long id);
List<Book> findAll();
Book findById(Long id);
}
6.创建Repository的实现类
其中findById方法和findAll的查询结果需要进行封装,其中findById使用了new BeanPropertyRowMapper(Book.class)对返回结果进行了封装,它可自动将一行数据映射到指定类的实例中,首先先将这个类实例化,然后通过名称匹配的方式,映射到属性中去。findAll方法的返回结果通过创建的内部类BookRowMapper进行封装。
@Repository
public class BookRepositoryImpl implements BookRepository {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public int save(Book book) {
return jdbcTemplate.update("insert into book(book_name,book_descript,book_price) value(?,?,?)",book.getBookName(),book.getBookDescript(),book.getBookPrice());
}
@Override
public int update(Book book) {
return 0;
}
@Override
public int delete(Long id) {
return 0;
}
@Override
public List<Book> findAll() {
return jdbcTemplate.query("select * from book",new BookRowMapper());
}
@Override
public Book findById(Long id) {
return jdbcTemplate.queryForObject("select * from book where book_id = ?",new Object[]{id},new BeanPropertyRowMapper<Book>(Book.class));
}
}
class BookRowMapper implements RowMapper<Book>{
@Override
public Book mapRow(ResultSet resultSet, int i) throws SQLException {
Book book = new Book();
book.setBookId(resultSet.getLong("book_id"));
book.setBookName(resultSet.getString("book_name"));
book.setBookDescript(resultSet.getString("book_descript"));
book.setBookPrice(resultSet.getFloat("book_price"));
return book;
}
}
7.测试
@SpringBootTest
class D8ApplicationTests {
@Autowired
private BookRepository bookRepository;
@Test
void saveBook(){
Book book = new Book("西游记","西游记是一本xxx的小说",36);
bookRepository.save(book);
}
@Test
void findBookById(){
Long id = new Long(102);
Book book = bookRepository.findById(id);
System.out.println(book.getBookName());
System.out.println(book.getBookDescript());
System.out.println(book.getBookPrice());
}
@Test
void findAll(){
List<Book> bookList = bookRepository.findAll();
System.out.println(bookList.get(0).getBookName());
}
}
8.总结
感觉spring jdbc对结果集的处理不太好用