spring需要的4核心2依赖、spring-test、spring-aop
还需要c3p0,JDBC驱动、spring-jdbc、spring-tx
package com.spring_day3.jtDemo;
//java bean
public class User {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.spring_day3.jtDemo;
import java.util.List;
public interface UserDao {
//增
void save(User user);
//删
void delete(int id);
//改
void update(User user);
//查
User getById(int id);
int getTotalCount();
List getAll();
}
第一种不继承JdbcDaoSupport,依赖关系如图:由连接池注入JDBC模板,然后将JDBC模板注入Dao类
package com.spring_day3.jtDemo;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import javax.annotation.Resource;
//JDBC模板的增删改查
public class UserDaoImpl implements UserDao {
//注入没有成功 检查 xml
private JdbcTemplate jt;
public JdbcTemplate getJt() {
return jt;
}
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}
@Override
public void save(User user) {
String sql = "insert into springdemo_user values(null,?,?)";
//sql语句设置?即空值,通过update方法携带参数传值
jt.update(sql, user.getName(), user.getAge());
}
@Override
public void delete(int id) {
String sql = "delete * from springdemo_user where id = ?";
jt.update(sql, id);
}
@Override
public void update(User user) {
String sql = "update springdemo_user set name = ? where id = ?";
jt.update(sql, user.getName(), user.getId());
}
@Override
public User getById(int id) {
String sql = "select * from springdemo_user where id = ?";
return jt.queryForObject(sql, new RowMapper() {
//查询有返回值才会进入该内部类
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
return user;
}
}, id);
}
@Override
public int getTotalCount() {
String sql = "select count(*) from springdemo_user";
//告诉jt将返回结果封装到Integer的类对象中
Integer count = jt.queryForObject(sql, Integer.class);
return count;
}
@Override
public List getAll() {
String sql = "select * from springdemo_user";
List list = jt.query(sql, new RowMapper() {
//查询有返回值才会进入该内部类
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
return user;
}
});
return list;
}
}
完成嵌套依赖注入。
注意:只要是属性最好都要设置setter、getter方法,防止可能需要在xml配置时忘了。我就是忘了设置jdbcTemplate属性始终无法注入。
dataSource属性的值当然也可以直接写在value中,此处为了不写死,另外应用了properties配置文件。
properties文件采用键值对的形式书写。属性名称前面加上jdbc是为了避免和关键词冲突。(比如user我就觉得到处都是这个词)
jdbc.jdbcUrl=jdbc:mysql:///springdemo
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
第2种继承JdbcDaoSupport :JDBCDaoSupport中已经创建了JDBC模板,可以在Dao层继承以后直接注入连接池dataSource
JdbcDaoSupport中已经准备好了JdbcTemplate模板所以不许要我们自己再创建。直接注入就ok了。
和上面对比不相同的地方就是没有JdbcTemplate属性的注入。jt也变成了直接由调用父类的super.getJdbcTemplate()。
package com.spring_day3.jtDemo;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
//JDBC模板的增删改查
public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
@Override
public void save(User user) {
String sql = "insert into springdemo_user values(null,?,?)";
//sql语句设置?即空值,通过update方法携带参数传值
super.getJdbcTemplate().update(sql, user.getName(), user.getAge());
}
@Override
public void delete(int id) {
String sql = "delete * from springdemo_user where id = ?";
super.getJdbcTemplate().update(sql, id);
}
@Override
public void update(User user) {
String sql = "update springdemo_user set name = ? where id = ?";
super.getJdbcTemplate().update(sql, user.getName(), user.getId());
}
@Override
public User getById(int id) {
String sql = "select * from springdemo_user where id = ?";
return super.getJdbcTemplate().queryForObject(sql, new RowMapper() {
//查询有返回值才会进入该内部类
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
return user;
}
}, id);
}
@Override
public int getTotalCount() {
String sql = "select count(*) from springdemo_user";
//告诉jt将返回结果封装到Integer的类对象中
Integer count = super.getJdbcTemplate().queryForObject(sql, Integer.class);
return count;
}
@Override
public List getAll() {
String sql = "select * from springdemo_user";
List list = super.getJdbcTemplate().query(sql, new RowMapper() {
//查询有返回值才会进入该内部类
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
return user;
}
});
return list;
}
}
package com.spring_day3.jtDemo;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//演示JDBC模板
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:applicationContext.xml")
public class Demo {
@Resource(name = "UserDaoImpl")
private UserDao ud;
@Test
public void demo1() {
User user = new User();
user.setName("xuan");
user.setAge(20);
ud.save(user);
}
}