一、数据持久层分析
数据连接层:
1.JDBC1.0数据连接层
(使用JDBC1.0的API连接数据库 )
2.JDBC2.0
(使用JDBC2.0的API封装的数据源连接数据库 )
C3P0连接池
DBCP连接池
数据操作层:
1.JDBC1.0数据操作层
(使用JDBC1.0的API操作数据库 )
2.JDBC2.0数据操作层
(使用QueryRunner操作数据库 )
(使用JdbcTemplate操作数据库 )
1.利用JDBC1.0连接数据库并操作CURD
2.利用DbUtils组件的QryRunner操作数据库(无事务控制的支持)
3.利用连接池管理连接资源
4.利用hibernate连接数据库并操作CURD
5.利用spring连接数据库
二、SpringJDBC
SpringJDBC 提供了数据连接层和数据操作层的支持。
数据连接层 -- 利用spring创建数据源对象。
数据操作层 -- 利用JdbcTemplate操作数据
Spring对JDBC提供了很好的支持。
体现在:
1)Spring对c3p0连接池的支持。
可以支持很多连接池的参数配置
(c3p0连接池有很多属性,
hibernate对c3p0连接池的支持属性只有几个。
而spring可以支持很多)
2)spring对jdbc提供了JcbcTemplate来简化jdbc操作。
JdbcTemplate模板工具类,类似于DbUtils工具类(QueryRunner)。
二、SpringJDBC提供了数据连接层的支持
Spring对jdbc的支持实质上是
1.利用Spring的IOC,控制反转创建数据源对象
2.利用Spring的DI,实现属性注入、赋值等操作。
Spring对Jdbc的支持主要是利用Spring容器可以创建对象的功能。
Spring对C3P0连接池有很好的支持
2.1 直接注入Spring创建的数据源对象
>>>>>> applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="6"></property>
<property name="maxIdleTime" value="1000"></property>
</bean>
<bean id="userDao02" class="org.jsoft.e_jdbc.UserDao02">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
>>>>>> UserDao02.java
public class UserDao02 {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void save() {
Statement st=null;
Connection conn=null;
try {
String sql="insert into t_dept(t_id,t_name) values('331','xxxx')";
conn = dataSource.getConnection();
st=conn.createStatement();
st.execute(sql);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
st.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("org/jsoft/e_jdbc/applicationContext.xml");
UserDao02 u=(UserDao02) ac.getBean("userDao02");
u.save();
}
}
2.2 直接注入含有Spring创建的数据源对象JdbcTemplate对象
>>>>>> applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="6"></property>
<property name="maxIdleTime" value="1000"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userDao04" class="org.jsoft.e_jdbc.UserDao04">
<property name="jt" ref="jdbcTemplate"></property>
</bean>
</beans>
>>>>>> UserDao04.java
public class UserDao04 {
private JdbcTemplate jt;
public void setJt(JdbcTemplate jdbcTemplate) {
this.jt = jdbcTemplate;
}
public void save() {
String sql="insert into t_dept(t_id,t_name) values('1331','xxxx')";
jt.update(sql);
}
public void update() {
String sql="update t_dept set t_name=? where t_id=?";
jt.update(sql, "xxxwww",1331);
}
三、SpringJDBC提供了数据操作层的支持
3.1 JdbcTemplate的API
+++ 创建JdbcTemplate对象
方式1:
JdbcTemplate jt=new JdbcTemplate (dataSource)
方式2:
JdbcTemplate jt=new JdbcTemplate ()
jt.setDataSource(dataSource)
+++ 方法
新增:
String sql="insert into t_dept(t_id,t_name) values('1331','xxxx')";
jt.update(sql);
修改:
String sql="update t_dept set t_name=? where t_id=?";
jt.update(sql, "xxxwww",1331)
删除:
String sql="delete from t_dept where t_id=?";
jt.update(sql, 1331);
查询单行数据:
String sql="select * from t_dept where t_id=?";
Map<String, Object> result = jt.queryForMap(sql, 1331);
查询多行数据:
String sql="select * from t_dept";
List<Map<String, Object>> result = jt.queryForList(sql);
查询数据,并封装对象
String sql="select * from t_dept where t_id=?";
List<DeptEntity> result = jt.query(sql, new RowMapper<DeptEntity>() {
@Override
public DeptEntity mapRow(ResultSet rs, int index) throws SQLException {
DeptEntity dept=new DeptEntity();
dept.setId(rs.getString("t_id"));
dept.setName(rs.getString("t_name"));
dept.setCode(rs.getString("t_code"));
DeptEntity dept2=new DeptEntity();
dept2.setId(rs.getString(1));
dept.setName(rs.getString(2));
dept.setCode(rs.getString(3));
return dept2;
}
},1331);
3.2 利用JdbcTemplate的构造方法创建含有数据源的对象
>>>>>> applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="6"></property>
<property name="maxIdleTime" value="1000"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
</bean>
<bean id="userDao04" class="org.jsoft.e_jdbc.UserDao04">
<property name="jt" ref="jdbcTemplate"></property>
</bean>
</beans>
>>>>>> UserDao04.java
public class UserDao04 {
private JdbcTemplate jt;
public void setJt(JdbcTemplate jdbcTemplate) {
this.jt = jdbcTemplate;
}
public void save() {
String sql="insert into t_dept(t_id,t_name) values('1331','xxxx')";
jt.update(sql);
}
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("org/jsoft/e_jdbc/applicationContext.xml");
UserDao04 u=(UserDao04) ac.getBean("userDao04");
u.query();
}
}
3.3 利用JdbcTemplate的setDataSource()方法创建含有数据源的对象
>>>>>> applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="6"></property>
<property name="maxIdleTime" value="1000"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userDao04" class="org.jsoft.e_jdbc.UserDao04">
<property name="jt" ref="jdbcTemplate"></property>
</bean>
</beans>
>>>>>> UserDao04.java
public class UserDao04 {
private JdbcTemplate jt;
public void setJt(JdbcTemplate jdbcTemplate) {
this.jt = jdbcTemplate;
}
public void save() {
String sql="insert into t_dept(t_id,t_name) values('1331','xxxx')";
jt.update(sql);
}
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("org/jsoft/e_jdbc/applicationContext.xml");
UserDao04 u=(UserDao04) ac.getBean("userDao04");
u.query();
}
}
3.4 示例代码
>>>>>> applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="6"></property>
<property name="maxIdleTime" value="1000"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
</bean>
<bean id="userDao04" class="org.jsoft.e_jdbc.UserDao04">
<property name="jt" ref="jdbcTemplate"></property>
</bean>
</beans>
>>>>>> UserDao04.java
public class UserDao04 {
private JdbcTemplate jt;
public void setJt(JdbcTemplate jdbcTemplate) {
this.jt = jdbcTemplate;
}
public void save() {
String sql="insert into t_dept(t_id,t_name) values('1331','xxxx')";
jt.update(sql);
}
public void update() {
String sql="update t_dept set t_name=? where t_id=?";
jt.update(sql, "xxxwww",1331);
}
public void delete() {
String sql="delete from t_dept where t_id=?";
jt.update(sql, 1331);
}
public void findById() {
String sql="select * from t_dept where t_id=?";
Map<String, Object> result = jt.queryForMap(sql, 1331);
System.out.println(result);
}
public void querys() {
String sql="select * from t_dept";
List<Map<String, Object>> result = jt.queryForList(sql);
System.out.println(result);
}
public void query() {
String sql="select * from t_dept where t_id=?";
List<DeptEntity> result = jt.query(sql, new RowMapper<DeptEntity>() {
@Override
public DeptEntity mapRow(ResultSet rs, int index) throws SQLException {
DeptEntity dept=new DeptEntity();
dept.setId(rs.getString("t_id"));
dept.setName(rs.getString("t_name"));
dept.setCode(rs.getString("t_code"));
DeptEntity dept2=new DeptEntity();
dept2.setId(rs.getString(1));
dept.setName(rs.getString(2));
dept.setCode(rs.getString(3));
return dept2;
}
},1331);
System.out.println(result);
}
public void query2() {
String sql="select * from t_dept";
List<DeptEntity> result = jt.query(sql, new RowMapper<DeptEntity>() {
@Override
public DeptEntity mapRow(ResultSet rs, int index) throws SQLException {
DeptEntity dept=new DeptEntity();
dept.setId(rs.getString("t_id"));
dept.setName(rs.getString("t_name"));
dept.setCode(rs.getString("t_code"));
DeptEntity dept2=new DeptEntity();
dept2.setId(rs.getString(1));
dept.setName(rs.getString(2));
dept.setCode(rs.getString(3));
return dept2;
}
});
System.out.println(result);
}
public void query3() {
String sql="select * from t_dept";
List<DeptEntity> result = jt.query(sql, new DeptRow());
System.out.println(result);
}
class DeptRow implements RowMapper<DeptEntity>{
@Override
public DeptEntity mapRow(ResultSet rs, int arg1) throws SQLException {
DeptEntity dept=new DeptEntity();
dept.setId(rs.getString("t_id"));
dept.setName(rs.getString("t_name"));
dept.setCode(rs.getString("t_code"));
return dept;
}
}
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("org/jsoft/e_jdbc/applicationContext.xml");
UserDao04 u=(UserDao04) ac.getBean("userDao04");
u.query();
}
}
四、SpringJDBC的演变过程
2.1 手动创建数据源
>>>>>> applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userDao01" class="org.jsoft.e_jdbc.UserDao01"></bean>
</beans>
>>>>>> UserDao01.java
public class UserDao01 {
public void save() {
Statement st=null;
Connection conn=null;
try {
String sql="insert into t_dept(t_id,t_name) values('33','xxxx')";
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
st=conn.createStatement();
st.execute(sql);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
st.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("org/jsoft/e_jdbc/applicationContext.xml");
UserDao01 u=(UserDao01) ac.getBean("userDao01");
u.save();
}
}
2.2 直接注入Spring创建的数据源
>>>>>> applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="6"></property>
<property name="maxIdleTime" value="1000"></property>
</bean>
<bean id="userDao02" class="org.jsoft.e_jdbc.UserDao02">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
>>>>>> UserDao02.java
public class UserDao02 {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void save() {
Statement st=null;
Connection conn=null;
try {
String sql="insert into t_dept(t_id,t_name) values('331','xxxx')";
conn = dataSource.getConnection();
st=conn.createStatement();
st.execute(sql);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
st.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("org/jsoft/e_jdbc/applicationContext.xml");
UserDao02 u=(UserDao02) ac.getBean("userDao02");
u.save();
}
}
2.3 直接注入Spring创建的数据源,并使用JdbcTemplate简化操作
>>>>>> applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="6"></property>
<property name="maxIdleTime" value="1000"></property>
</bean>
<bean id="userDao03" class="org.jsoft.e_jdbc.UserDao03">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
>>>>>> UserDao03.java
public class UserDao03 {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void save() {
String sql="insert into t_dept(t_id,t_name) values('1331','xxxx')";
JdbcTemplate jt=new JdbcTemplate(dataSource);
jt.update(sql);
}
public void update() {
String sql="update t_dept set t_name=? where t_id=?";
JdbcTemplate jt=new JdbcTemplate(dataSource);
jt.update(sql, "xxxwww",1331);
}
public void delete() {
String sql="delete from t_dept where t_id=?";
JdbcTemplate jt=new JdbcTemplate(dataSource);
jt.update(sql, 1331);
}
public void findById() {
String sql="select * from t_dept where t_id=?";
JdbcTemplate jt=new JdbcTemplate(dataSource);
Map<String, Object> result = jt.queryForMap(sql, 1331);
System.out.println(result);
}
public void querys() {
String sql="select * from t_dept";
JdbcTemplate jt=new JdbcTemplate(dataSource);
List<Map<String, Object>> result = jt.queryForList(sql);
System.out.println(result);
}
public void query() {
String sql="select * from t_dept where t_id=?";
JdbcTemplate jt=new JdbcTemplate(dataSource);
List<DeptEntity> result = jt.query(sql, new RowMapper<DeptEntity>() {
@Override
public DeptEntity mapRow(ResultSet rs, int index) throws SQLException {
DeptEntity dept=new DeptEntity();
dept.setId(rs.getString("t_id"));
dept.setName(rs.getString("t_name"));
dept.setCode(rs.getString("t_code"));
DeptEntity dept2=new DeptEntity();
dept2.setId(rs.getString(1));
dept.setName(rs.getString(2));
dept.setCode(rs.getString(3));
return dept2;
}
},1331);
System.out.println(result);
}
public void query2() {
String sql="select * from t_dept";
JdbcTemplate jt=new JdbcTemplate(dataSource);
List<DeptEntity> result = jt.query(sql, new RowMapper<DeptEntity>() {
@Override
public DeptEntity mapRow(ResultSet rs, int index) throws SQLException {
DeptEntity dept=new DeptEntity();
dept.setId(rs.getString("t_id"));
dept.setName(rs.getString("t_name"));
dept.setCode(rs.getString("t_code"));
DeptEntity dept2=new DeptEntity();
dept2.setId(rs.getString(1));
dept.setName(rs.getString(2));
dept.setCode(rs.getString(3));
return dept2;
}
});
System.out.println(result);
}
public void query3() {
String sql="select * from t_dept";
JdbcTemplate jt=new JdbcTemplate(dataSource);
List<DeptEntity> result = jt.query(sql, new DeptRow());
System.out.println(result);
}
class DeptRow implements RowMapper<DeptEntity>{
@Override
public DeptEntity mapRow(ResultSet rs, int arg1) throws SQLException {
DeptEntity dept=new DeptEntity();
dept.setId(rs.getString("t_id"));
dept.setName(rs.getString("t_name"));
dept.setCode(rs.getString("t_code"));
return dept;
}
}
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("org/jsoft/e_jdbc/applicationContext.xml");
UserDao03 u=(UserDao03) ac.getBean("userDao03");
u.delete();
}
}
2.4 直接注入含有spring创建的数据源的JdbcTemplate
>>>>>> applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="6"></property>
<property name="maxIdleTime" value="1000"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
</bean>
<bean id="userDao04" class="org.jsoft.e_jdbc.UserDao04">
<property name="jt" ref="jdbcTemplate"></property>
</bean>
</beans>
>>>>>> UserDao04.java
public class UserDao04 {
private JdbcTemplate jt;
public void setJt(JdbcTemplate jdbcTemplate) {
this.jt = jdbcTemplate;
}
public void save() {
String sql="insert into t_dept(t_id,t_name) values('1331','xxxx')";
jt.update(sql);
}
public void update() {
String sql="update t_dept set t_name=? where t_id=?";
jt.update(sql, "xxxwww",1331);
}
public void delete() {
String sql="delete from t_dept where t_id=?";
jt.update(sql, 1331);
}
public void findById() {
String sql="select * from t_dept where t_id=?";
Map<String, Object> result = jt.queryForMap(sql, 1331);
System.out.println(result);
}
public void querys() {
String sql="select * from t_dept";
List<Map<String, Object>> result = jt.queryForList(sql);
System.out.println(result);
}
public void query() {
String sql="select * from t_dept where t_id=?";
List<DeptEntity> result = jt.query(sql, new RowMapper<DeptEntity>() {
@Override
public DeptEntity mapRow(ResultSet rs, int index) throws SQLException {
DeptEntity dept=new DeptEntity();
dept.setId(rs.getString("t_id"));
dept.setName(rs.getString("t_name"));
dept.setCode(rs.getString("t_code"));
DeptEntity dept2=new DeptEntity();
dept2.setId(rs.getString(1));
dept.setName(rs.getString(2));
dept.setCode(rs.getString(3));
return dept2;
}
},1331);
System.out.println(result);
}
public void query2() {
String sql="select * from t_dept";
List<DeptEntity> result = jt.query(sql, new RowMapper<DeptEntity>() {
@Override
public DeptEntity mapRow(ResultSet rs, int index) throws SQLException {
DeptEntity dept=new DeptEntity();
dept.setId(rs.getString("t_id"));
dept.setName(rs.getString("t_name"));
dept.setCode(rs.getString("t_code"));
DeptEntity dept2=new DeptEntity();
dept2.setId(rs.getString(1));
dept.setName(rs.getString(2));
dept.setCode(rs.getString(3));
return dept2;
}
});
System.out.println(result);
}
public void query3() {
String sql="select * from t_dept";
List<DeptEntity> result = jt.query(sql, new DeptRow());
System.out.println(result);
}
class DeptRow implements RowMapper<DeptEntity>{
@Override
public DeptEntity mapRow(ResultSet rs, int arg1) throws SQLException {
DeptEntity dept=new DeptEntity();
dept.setId(rs.getString("t_id"));
dept.setName(rs.getString("t_name"));
dept.setCode(rs.getString("t_code"));
return dept;
}
}
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("org/jsoft/e_jdbc/applicationContext.xml");
UserDao04 u=(UserDao04) ac.getBean("userDao04");
u.query();
}
}