Spring jdbcTemplate

栏目: Java · 发布时间: 8年前

内容简介:Spring jdbcTemplate

一,Spring注解Aop开发

tip:1.只需要在applicationContext.xml中添加AOP的自动代理就行

--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

2.不需要在被增强类上做什么处理,只需要 在增强类上添加注解就行

@Aspect -->添加在增强类上,标识开启注解

@After,@Around,@Before,等增强标识,而且只有环绕增强需要有参执行方法 new ProceedingJoinPoint().proceed(); (interface)

3. applicationContext.xml

<!-- 开启aop注解操作(自动代理) -->
			<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
			<bean id="book" class="com.gog.aop.Book"></bean>
			<bean id="myBook" class="com.gog.aop.MyBook"></bean>

4.增强类MyBook

@Aspect
			public class MyBook {
				@Before(value="execution(* com.gog.aop.Book.add(..))")
				public void before1() {
					System.out.println("before--->");
				}			
				@After(value="execution ( * com.gog.aop.Book.add())")
				public void after1() {
					System.out.println("after---->");
				}
				@Around(value="execution ( * com.gog.aop.Book.add())")
				public void around1(ProceedingJoinPoint point) throws Throwable{
					System.out.println("around1");
					point.proceed();
					System.out.println("around2");
				}

二,spring 的JdbcTemplate 模板

1.spring-jdbc-3.2.0.RELEASE.jar 和 spring-tx-3.2.0.RELEASE.jar mysql-connector-java-5.0.8-bin.jar

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/message   oraclejdbc:mysql:///message  
user=root
password=


driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:XE
user=oracle
password=oracle
2.增删改  -->使用的都是 new JdbcTemplate().update();方法
a.共同步骤
// 1.设置数据库信息
DriverManagerDataSource source = new DriverManagerDataSource();
source.setDriverClassName("com.mysql.jdbc.Driver");
// 两个都有效
// jdbc:mysql://127.0.0.1:3306/message
// jdbc:mysql://message
source.setUrl("jdbc:mysql:///message");
source.setUsername("root");
source.setPassword("");
// 2.建立jdbcTemplate对象,设置数据源
JdbcTemplate jdbcTemplate = new JdbcTemplate(source);
b.增加
String sql = " insert into message values(?,?,?,?)";
int rows = jdbcTemplate.update(sql, 7, "GGr", "3", "4");
System.out.println(rows);
c.删除
String sql = "delete from  message  where id =?";
int update = jdbcTemplate.update(sql,7);
ystem.out.println(update);
d.更新
String sql = "update message set command=? where id =?";
int update = jdbcTemplate.update(sql,"ECJTU",7);
System.out.println(update);
3.查询
jdbcTemplate 模板实现查询有接口RowMapper,但jdbcTemplate 没有提供实现类,所以不同类型的数据需要自己来进行封装

三,最原始的JDBC操作  --->进行数据的查询操作 使用的类全是java.sql下的

@Test
public void Jdbc() throws SQLException {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 创立连接
conn =  DriverManager.getConnection("jdbc:mysql:///message", "root", "");
String ssql = "select * from message where id > ?";
//预编译 sql 语句
pst =  conn.prepareStatement(ssql);
//设置参数值
pst.setLong(1, 1);
//执行sql语句 --> 返回一个结果集
rs = pst.executeQuery();
//遍历结果集
while (rs.next()) {
int id = rs.getInt("id");
String command = rs.getString("command");
String CONTENT = rs.getString("CONTENT");
String DESCRIPTION = rs.getString("DESCRIPTION");
message m = new message(id, command, CONTENT, DESCRIPTION);
System.out.println(m);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
//关闭
rs.close(); pst.close(); conn.close();
}
}

四, jdbcTemplate 的查询

1.查询结果集是一个对象

a.查询返回结果对象 queryForObject();

@Test
public void SelectObject(){
// 1.设置数据库信息
DriverManagerDataSource source = new DriverManagerDataSource();
source.setDriverClassName("com.mysql.jdbc.Driver");
source.setUrl("jdbc:mysql:///message");
source.setUsername("root");
source.setPassword("");
// 2.建立jdbcTemplate对象,设置数据源
JdbcTemplate jdbcTemplate = new JdbcTemplate(source);
//3.调用修改模板
String sql = "select * from message where id = ?";
/* 
* 1.sql sql语句
*  2. 对结构集中的数据进行封装
*  3.可变参数,对应sql语句种的“?”
* */
message m = jdbcTemplate.queryForObject(sql, new MyRowMapper(), 1);
System.out.println(m);

}
b.数据封装类MyRowMapper.java
class MyRowMapper implements RowMapper<message>{
//这个只封装到返回结果集,但对结构集没有进行在封装
@Override
public message mapRow(ResultSet rs, int num) throws SQLException {
//1.从结果集中的数据得到
int id = rs.getInt("id");
String command = rs.getString("command");
String CONTENT = rs.getString("CONTENT");
String DESCRIPTION = rs.getString("DESCRIPTION");
//2.数据的封装
message m = new message(id, command, CONTENT, DESCRIPTION);
return m;
}

2.查询多个对象的集合: 

a.query() 这个方法

b.还需要对数据集里面的数据进行封装MyRowMapper();

List<message> s = jdbcTemplate.query(sql, new MyRowMapper());
System.out.println(s);

Spring jdbcTemplate


以上所述就是小编给大家介绍的《Spring jdbcTemplate》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Art and Science of CSS

The Art and Science of CSS

Jonathan Snooks、Steve Smith、Jina Bolton、Cameron Adams、David Johnson / SitePoint / March 9, 2007 / $39.95

Want to take your CSS designs to the next level? will show you how to create dozens of CSS-based Website components. You'll discover how to: # Format calendars, menus and table of contents usin......一起来看看 《The Art and Science of CSS》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具