Spring JdbcTemplate类和log4J

Spring JdbcTemplate类

Spring 针对数据库开发提供了JdbcTemplate类,该类封装了 JDBC,支持对数据库的所有操作。

JdbcTemplate位于spring-jdbc-x.x.x.jar包中,其全限定命名为org.springframework.jdbc.core.JdbcTemplate。此外使用JdbcTemplate还需要导入spring-tx-x.x.x.jar包,该包用来处理事务和异常。

在 Spring 中,JDBC 的相关信息在配置文件中完成。

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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http:/www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--数据库驱动-->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!--连接数据库的url-->
<property name= "url" value="jdbc:mysql://localhost/xx" />
<!--连接数据库的用户名-->
<property name="username" value="root" />
<!--连接数据库的密码-->
<property name="password" value="root" />
</bean>
<!--配置JDBC模板-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--默认必须使用数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置注入类-->
<bean id="xxx" class="xxx">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
...
</beans>

上述代码中定义了 3 个Bean,分别是dataSourcejdbcTemplate和需要注入类的Bean。其中dataSource对应的是DriverManagerDataSource类,用于对数据源进行配置;jdbcTemplate对应JdbcTemplate类,该类中定义了JdbcTemplate的相关配置。

dataSource中,定义了 4 个连接数据库的属性。

属性名 说明
driverClassName 所使用的驱动名称,对应驱动 JAR 包中的 Driver 类
url 数据源所在地址
username 访问数据库的用户名
password 访问数据库的密码

上表中的属性值需要根据数据库类型或者机器配置的不同进行相应设置。如果数据库类型不同,则需要更改驱动名称。如果数据库不在本地,则需要将localhost替换成相应的主机 IP。

在定义JdbcTemplate时,需要将dataSource注入到JdbcTemplate中。而在其他的类中要使用JdbcTemplate,也需要将JdbcTemplate注入到使用类中(通常注入dao类中)。

JdbcTemplate类中,提供了大量的查询和更新数据库的方法,如query()、update()等。

方法 说明
public int update(String sql)
public int update(String sql,Object… args)
用于执行新增、修改、删除等语句
args 表示需要传入到 query 中的参数
public void execute(String sql)
public T execute(String sql, PreparedStatementCallback action)
可以执行任意 SQL,一般用于执行 DDL 语句
action 表示执行完 SQL 语句后,要调用的函数
public T query(String sql, ResultSetExtractor rse)
public List query(String sql, RowMapper rse)
用于执行查询语句
以 ResultSetExtractor 作为参数的 query 方法返回值为 Object,使用查询结果需要对其进行强制转型
以 RowMapper 作为参数的 query 方法返回值为 List

示例

步骤如下:

  • 创建SpringDemo项目,并在src目录下创建net.biancheng包。
  • 导入 Spring 相关 JAR 包及mysql-connector-java.x.x.x.jar包。
  • net.biancheng包下创建User、UserDao、UserDaoImpl、Beans.xmlMainApp
  • 运行 SpringDemo 项目。
1
2
3
4
5
6
7
8
9
10
11
12
13
package net.biancheng;
public class User {
private int id;
private String name;
private int age;
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
// 省略set和get方法
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package net.biancheng;
import java.util.List;
public interface UserDao {
/**
* 初始化User表
*/
void createUserTable();
/**
* 保存用户
*/
void saveUser(User user);
/**
* 查询用户
*/
List<User> listUser();
}
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
package net.biancheng;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
public class UserDaoImpl implements UserDao {
private JdbcTemplate jdbcTemplate;
private UserDao userDao;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void setDataSource(DataSource datasource) {
this.jdbcTemplate = new JdbcTemplate(datasource);
}
@Override
public void createUserTable() {
this.jdbcTemplate.execute("CREATE TABLE `user` (\r\n" + " `id` int(11) NOT NULL AUTO_INCREMENT,\r\n"
+ " `name` varchar(50) DEFAULT NULL,\r\n" + " `age` int(11) DEFAULT NULL,\r\n"
+ " PRIMARY KEY (`id`)\r\n" + ") ENGINE=MyISAM DEFAULT CHARSET=utf8;");
}
@Override
public void saveUser(User user) {
this.jdbcTemplate.update("INSERT INTO USER(NAME,age) VALUES (?,?)", user.getName(), user.getAge());
}
@Override
public List<User> listUser() {
List<User> users = this.jdbcTemplate.query("SELECT NAME,age FROM USER", new RowMapper<User>() {
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
return user;
}
});
return users;
}
}
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
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--数据库驱动 -->
<property name="driverClassName"
value="com.mysql.jdbc.Driver" />
<!--连接数据库的url -->
<property name="url" value="jdbc:mysql://localhost/test" />
<!--连接数据库的用户名 -->
<property name="username" value="root" />
<!--连接数据库的密码 -->
<property name="password" value="root" />
</bean>
<!--配置JDBC模板 -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<!--默认必须使用数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="userdao" class="net.biancheng.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package net.biancheng;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("Beans.xml");
UserDao dao = (UserDao) ctx.getBean("userdao");
dao.createUserTable();
dao.saveUser(new User("bianchengbang", 12));
dao.saveUser(new User("baidu", 18));
List<User> users = dao.listUser();
for (User user : users) {
System.out.println("姓名:" + user.getName() + "\t年龄:" + user.getAge());
}
}
}

运行结果如下。

1
2
姓名:bianchengbang 年龄:12
姓名:baidu 年龄:18

Spring集成Log4J

示例

步骤如下:

  • 创建SpringDemo项目,并在 src 目录下创建net.biancheng包。
  • 导入 Spring 相关 JAR 包及log4j-x.y.z.jar
  • net.biancheng包下创建HelloWorld、MainApp、Beans.xmllog4j.properties
  • 运行SpringDemo项目。
1
2
3
4
5
6
7
8
9
10
package net.biancheng;
public class HelloWorld {
private String message;
public void setMessage(String message) {
this.message = message;
}
public void getMessage() {
System.out.println("消息:" + message);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package net.biancheng;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
static Logger log = Logger.getLogger(MainApp.class.getName());
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
log.info("Going to create HelloWord Obj");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
log.info("Exiting the program");
}
}
1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="helloWorld" class="net.biancheng.HelloWorld">
<property name="message" value="Hello,bianchengbang!" />
</bean>
</beans>

log4j.properties 内容如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=D:\\log.out

# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug

# Set the append to false, overwrite
log4j.appender.FILE.Append=false

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

运行结果:

1
消息:Hello,bianchengbang!

log.out文件内容:

1
2
Going to create HelloWord Obj
Exiting the program
打赏
  • Copyrights © 2017-2023 WSQ
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信