回顾:
JdbcTemplate实现CRUD操作
在dao中定义JdbcTemplate的方式的方式,在持久层每个实现类中要重复写注入JdbcTemplate的代码。而dao继承JdbcDaoSupport的方式,JdbcDaoSupport类已经提供了set注入的方法,就可以不用写注入JdbcTemplate的set方法了。
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.0.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.0.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>5.0.2.RELEASEversion>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.6version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.0.2.RELEASEversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
dependencies>
//用户持久层接口
public interface UserDao {
//增加数据
void add(User user);
//根据id查询用户
User getById(Integer id);
}
//用户持久层接口实现类
public class UserDaoImpl implements UserDao {
private JdbcTemplate jdbcTemplate;
//类中提供需要注入成员的set方法
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void add(User user) {
jdbcTemplate.update("insert into user(id,username,birthday,sex,address) values (?,?,?,?,?)", user.getId(),user.getUsername(),user.getBirthday(),user.getSex(),user.getAddress());
}
public User getById(Integer id) {
return jdbcTemplate.queryForObject("select * from user where id=?", new BeanPropertyRowMapper<User>(User.class), id);
}
}
//用户业务层接口
public interface UserService {
//增加数据
void add(User user);
//根据id查询用户
User getById(Integer id);
}
//用户业务层接口实现类
public class UserServiceImpl implements UserService {
private UserDao userDao;
//提供set注入
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(User user) {
userDao.add(user);
}
public User getById(Integer id) {
User user = userDao.getById(id);
return user;
}
}
加载配置文件的三种方式
将数据库连接的信息配置到属性配置文件中
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/spring
uname=root
pwd=root
<bean id="userService" class="com.mycode.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao">property>
bean>
<bean id="userDao" class="com.mycode.dao.impl.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate">property>
bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource">property>
bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}">property>
<property name="url" value="${url}">property>
<property name="username" value="${uname}">property>
<property name="password" value="${pwd}">property>
bean>
<bean id="userService" class="com.mycode.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao">property>
bean>
<bean id="userDao" class="com.mycode.dao.impl.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate">property>
bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource">property>
bean>
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}">property>
<property name="url" value="${url}">property>
<property name="username" value="${uname}">property>
<property name="password" value="${pwd}">property>
bean>
多个配置属性配置文件的情况下可与配置文件解析器方式
搭配使用
在类路径下添加一个属性配置文件jdbc_config.properties
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://127.0.0.1:3306/spring
dataSource.username=root
dataSource.password=root
#前缀 dataSource 是指定Bean的id,给该Bean注入指定属性信息
<bean id="userService" class="com.mycode.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao">property>
bean>
<bean id="userDao" class="com.mycode.dao.impl.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate">property>
bean>
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}">property>
<property name="url" value="${url}">property>
<property name="username" value="${uname}">property>
<property name="password" value="${pwd}">property>
bean>
<context:property-override location="jdbc_config.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource1">property>
bean>
@RunWith(SpringJUnit4ClassRunner.class)1.使用@RunWith注解替换原有运行器
@ContextConfiguration(locations= {"classpath:beans.xml"})//2.使用@ContextConfiguration指定spring配置文件的位置
public class SpringJdbcTemplateTest {
@Autowired//3.使用@Autowired给测试类中的变量注入数据
private UserService userService;
@Test
public void getByIdTest(){
User user = userService.getById(3);
System.out.println(user);
}
}
参考: Spring纯注解配置,使用配置类,替换xml配置
public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
public void add(User user) {
getJdbcTemplate().update("insert into user(id,username,birthday,sex,address) values (?,?,?,?,?)", user.getId(),user.getUsername(),user.getBirthday(),user.getSex(),user.getAddress());
}
public User getById(Integer id) {
return getJdbcTemplate().queryForObject("select * from user where id=?", new BeanPropertyRowMapper<User>(User.class), id);
}
}
<bean id="userService" class="com.mycode.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao">property>
bean>
<context:property-override location="jdbc_config.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" />
<bean id="userDao" class="com.mycode.dao.impl.UserDaoImpl">
<property name="dataSource" ref="dataSource">property>
bean>