Fork me on GitHub

Spring———简单增删改查

1
Spring————Jdbc增删改查

项目功能:使用spring框架依赖注入,控制反转等功能实现对数据库数据的增删改查功能

项目准备

创建项目

​ 1.项目背景:

​ Ecipse、jdk1.8 、Tomcat8.5、MySQL8.0

​ 2.创建项目:

​ 右击——>new Dynamic Web Project,然后在Java Resources中src下创建dao包、pojo包;

​ database.properties(连接数据库配置文件),log4j.properties(日志文件),applicationContext.xml(spring配置文件)。

导入jar包

​ 1.在WebContext中lib下导入主要用到的jar包

编写代码

配置文件代码

​ 1.database.properties代码

1
2
3
4
driverClass=com.mysql.cj.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/school?useSSL=false&serverTimezone=UTC
user=root
password=root

​ 2.log4j.properties代码(网上随便找的,暂时不知道什么功能)

1
2
3
4
5
log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
log4j.logger.org.exam=DEBUG
log4j.logger.org.springframework.beans.factory=INFO

​ 3.applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">

<context:property-placeholder location="classpath:database.properties"/>
<!-- 连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>

<!-- jdbcTemplate -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- AccountDao -->
<bean name="accountDao" class="net.zzqd.spring.dao.AccountDaoImpl" >
<property name="jt" ref="jdbcTemplate"></property>
</bean>


</beans>

​ 4.数据库字段:id (int) username (varchar) money(double)

业务代码

​ 1.实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package net.zzqd.spring.pojo;

/**
* @author zzq
* @version 创建时间:2019年5月31日 下午8:37:48
*
*/

public class Account
{
private Integer id;
private String username;
private double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
@Override
public String toString() {
return "Account [id=" + id + ", username=" + username + ", money=" + money + "]";
}


}

​ 2.AccountDao(Dao层代码)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package net.zzqd.spring.dao;

import java.util.List;
import net.zzqd.spring.pojo.Account;

/**
* @author zzq
* @version 创建时间:2019年5月31日 下午8:40:07
*
*/

public interface AccountDao
{
void save(Account account);

void delete(Integer id);

void update(Account account);

Account getById(Integer id);

List<Account> getAll();

int getTotalCount();
}

​ 3.AccountDaoImpl实现类代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package net.zzqd.spring.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import net.zzqd.spring.pojo.Account;

/**
* @author zzq
* @version 创建时间:2019年5月31日 下午8:43:49
* spring的增删该查(查重要)
*/

public class AccountDaoImpl implements AccountDao {

JdbcTemplate jt;
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}

@Override
public void save(Account account)
{
String sql="insert into account (username,money) values (?,?)";
jt.update(sql,account.getUsername(),account.getMoney());
}

@Override
public void delete(Integer id)
{
String sql="delete from account where id=?";
jt.update(sql,id);
}

@Override
public void update(Account account)
{
String sql="update account set username=?,money=? where id=?";
jt.update(sql,account.getUsername(),account.getMoney(),account.getId());
}

@Override
public Account getById(Integer id)
{
String sql="select * from account where id=?";
Account account = jt.queryForObject(sql, new RowMapper<Account>()
{
@Override
public Account mapRow(ResultSet rs, int index) throws SQLException
{
return mapRowHandler(rs);
}
},id);
return account;
}

@Override
public List<Account> getAll()
{
String sql="select * from account";
List<Account> list = jt.query(sql, new RowMapper<Account>()
{
@Override
public Account mapRow(ResultSet rs, int index) throws SQLException
{
return mapRowHandler(rs);
}
});
return list;
}

@Override
public int getTotalCount()
{
String sql = "select count(1) from account";
Integer count = jt.queryForObject(sql, Integer.class);
return count;
}

private Account mapRowHandler(ResultSet rs) throws SQLException
{
Account account = new Account();
account.setUsername(rs.getString("username"));
account.setMoney(rs.getDouble("money"));
account.setId(rs.getInt("id"));
return account;
}
}

项目测试

(一)在Java Resources下test包下创建测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package net.zzqd.spring.jdbctemplate;
import java.beans.PropertyVetoException;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import net.zzqd.spring.dao.AccountDao;
import net.zzqd.spring.pojo.Account;
import net.zzqd.spring.utils.DbUtils;


/**
* @author zzq
* @version 创建时间:2019年5月31日 下午6:45:20
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestJdbcTemplate
{
@Resource(name="accountDao")
private AccountDao accountDao;

@Test
public void testSave1()
{
Account account = new Account();
account.setUsername("Aemon");
account.setMoney(50000);
accountDao.save(account);

}

@Test
public void testDelete()
{
accountDao.delete(8);
}

@Test
public void testUpdate()
{
Account account = new Account();
account.setUsername("Bemon");
account.setMoney(40000);
account.setId(7);
accountDao.update(account);
}

@Test
public void testGetById()
{

Account account = accountDao.getById(7);
System.out.println(account.toString());
}

@Test
public void testGetAll()
{

List<Account> list = accountDao.getAll();
System.out.println(list);
}
@Test
public void testGetTotalCount()
{
int count = accountDao.getTotalCount();
System.out.println(count);
}
}

总结

传统程序设计上,都是主动创建用户类,用户信息类,将用户信息类主动注入到用户类,用户类依赖于用户信息类,当有了IoC(控制反转)、DI(依赖注入)容器后,在客户端不再主动创建这些对象了,由IoC容器创建创建用户类,并将依赖对象注入到用户类中,由用户信息类需要注入时,首先创建用户信息类,然后将其注入到用户类中,并由容器管理这些对象的生命周期。