依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-test
test
mysql
mysql-connector-java
runtime
数据源配置(项目根目录下application.properties中配置数据源信息)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.username=root
spring.datasource.password=123456
数据库初始化
CREATE DATABASE `demo`;
CREATE TABLE `sys_user` (
`id` varchar(255) NOT NULL,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Bean的创建
package com.vincent.bean;
public class SysUser {
private String id;
private String username;
private String password;
/** get and set ... */
}
定义DAO接口和实现类
- JdbcTemplate 可以直接用 @Autowired、@Resource 注入到Bean中使用
- 实现类中加上@Repository 注解
package com.vincent.dao;
import com.vincent.bean.SysUser;
import java.util.List;
public interface SysUserDao {
/**
* 查询全部
* @return
*/
List query();
/**
* 按ID查询
* @param id
* @return
*/
SysUser findById(String id);
/**
* 插入操作
* @param sysUser
* @return
*/
int insert(SysUser sysUser);
/**
* 修改操作
* @param sysUser
* @return
*/
int update(SysUser sysUser);
/**
* 删除所有数据
* @return
*/
int delete();
}
package com.vincent.dao.impl;
import com.vincent.bean.SysUser;
import com.vincent.dao.SysUserDao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.List;
@Repository
public class SysUserDaoImpl implements SysUserDao {
@Resource
private JdbcTemplate jdbcTemplate;
@Override
public List query() {
String sql = "select * from sys_user ";
RowMapper rowMapper = new BeanPropertyRowMapper<>(SysUser.class);
return jdbcTemplate.query(sql, rowMapper);
}
@Override
public SysUser findById(String id) {
String sql = "select * from sys_user where id = ? ";
Object[] objects = new Object[]{id};
RowMapper rowMapper = new BeanPropertyRowMapper<>(SysUser.class);
return jdbcTemplate.queryForObject(sql, objects, rowMapper);
}
@Override
public int insert(SysUser sysUser) {
String sql = "insert into sys_user (id, username, password) VALUES (?, ?, ?) ";
Object[] objects = new Object[]{sysUser.getId(), sysUser.getUsername(), sysUser.getPassword()};
return jdbcTemplate.update(sql, objects);
}
@Override
public int update(SysUser sysUser) {
String sql = "update sys_user set username = ?, password = ? where id = ? ";
Object[] objects = new Object[]{sysUser.getUsername(), sysUser.getPassword(), sysUser.getId()};
return jdbcTemplate.update(sql, objects);
}
@Override
public int delete() {
String sql = "delete from sys_user ";
return jdbcTemplate.update(sql);
}
}
定义Service接口和实现类
- 通过@Autowired、@Resource注解,注入DAO
- 实现类上加入@Service注解
package com.vincent.service;
import com.vincent.bean.SysUser;
import java.util.List;
public interface SysUserService {
/**
* 查询全部
* @return
*/
List query();
/**
* 按ID查询
* @param id
* @return
*/
SysUser findById(String id);
/**
* 插入操作
* @param sysUser
* @return
*/
int insert(SysUser sysUser);
/**
* 修改操作
* @param sysUser
* @return
*/
int update(SysUser sysUser);
/**
* 删除所有数据
* @return
*/
int delete();
}
package com.vincent.service.impl;
import com.vincent.bean.SysUser;
import com.vincent.dao.SysUserDao;
import com.vincent.service.SysUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class SysUserServiceImpl implements SysUserService{
@Resource
private SysUserDao sysUserDao;
@Override
public List query() {
return sysUserDao.query();
}
@Override
public SysUser findById(String id) {
return sysUserDao.findById(id);
}
@Override
public int insert(SysUser sysUser) {
return sysUserDao.insert(sysUser);
}
@Override
public int update(SysUser sysUser) {
return sysUserDao.update(sysUser);
}
@Override
public int delete() {
return sysUserDao.delete();
}
}
编写测试用例
package com.vincent.service;
import com.vincent.bean.SysUser;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.UUID;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class SysUserServiceTest {
@Resource
private SysUserService userService;
private SysUser sysUser = new SysUser(UUID.randomUUID().toString(), "superadmin", "superadmin");
@Before
public void setUp(){
userService.delete();
userService.insert(sysUser);
}
@Test
public void queryTest(){
Assert.assertNotNull(userService.query());
Assert.assertEquals(1, userService.query().size());
}
@Test
public void findByIdTest(){
Assert.assertEquals(sysUser.getUsername(), userService.findById(sysUser.getId()).getUsername());
}
@Test
public void insertTest(){
Assert.assertEquals(1, userService.insert(new SysUser(UUID.randomUUID().toString(), "superadmin", "superadmin")));
}
@Test
public void updateTest(){
sysUser.setUsername("admin");
sysUser.setPassword("admin");
Assert.assertEquals(1, userService.update(sysUser));
}
}
总结
- 在 pom.xml 中添加依赖
- 在 application.properties 中配置数据源
- 创建数据库和Java对象
- 编写DAO和Service层
官方API
JdbcTemplate