首先新建一个实体类,这里不需要构造方法
public class Account {
private String username;
private int money;
public void setUsername(String username){
this.username=username;
}
public void setMoney(int money){
this.money=money;
}
public String getUsername() {
return username;
}
public int getMoney() {
return money;
}
}
然后是Service接口及其实现类
public interface Service {
//修改数据
public void update(String username,int money) throws SQLException;
//新增数据
public void save(Account account) throws SQLException;
//删除数据
public void delete(String username) throws SQLException;
//查找数据
public List<Account> find() throws SQLException;
}
@Service("service")
public class ServiceImpl implements Service {
@Autowired//自动注入
private UserDao dao;
public void update(String username,int money) throws SQLException {
this.dao.update(username,money);
}
public void delete(String username) throws SQLException {
this.dao.delete(username);
}
public void save(Account account) throws SQLException {
this.dao.save(account);
}
public List<Account> find() throws SQLException {
return this.dao.find();
}
}
然后是持久层接口及其实现类
public interface UserDao {
//修改数据
public void update(String username,int money) throws SQLException;
//新增数据
public void save(Account account) throws SQLException;
//删除数据
public void delete(String username) throws SQLException;
//查找数据
public List<Account> find() throws SQLException;
}
@Repository("Dao")
public class UserDaoImpl implements UserDao {
@Autowired
private QueryRunner query;
public void update(String username,int money) throws SQLException {
this.query.update("update test set money=? where username=?",
money,username);
}
public void delete(String username) throws SQLException {
this.query.update("delete from test where username =?",username);
}
public List<Account> find() throws SQLException {
return this.query.query("select * from test",new BeanListHandler<Account>(Account.class));
}
public void save(Account account) throws SQLException {
this.query.insert("insert into test values(?,?)", new ResultSetHandler<Account>() {
public Account handle(ResultSet resultSet) throws SQLException {
return null;
}
},account.getUsername(),account.getMoney());
}
}
工具类
@PropertySource("classpath:jdbc.properties")
@ComponentScan(basePackages = {"Dao","Service"})//要扫描的包
@Import(JDBCConfig.class)
public class SpringConfiguation {
}
public class JDBCConfig {
@Bean(name="query")
@Scope("prototype")
public QueryRunner find(DataSource dataSource){
return new QueryRunner(dataSource);
}
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${jdbc.DriverClass}")
private String driverClass;
@Bean(name="datasource")
public DataSource createDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource=new ComboPooledDataSource();
dataSource.setDriverClass(driverClass);
dataSource.setUser(username);
dataSource.setPassword(password);
dataSource.setJdbcUrl(url);
return dataSource;
}
}
最后是properties文件
jdbc.url=jdbc:mysql://localhost:3306/db?serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
jdbc.DriverClass=com.mysql.cj.jdbc.Driver
新建数据库表
create table test(username varchar20,money int);
insert into test values('xx',1000)
insert into test values('yy',900)