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"
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-4.0.xsd
">
<!-- 配置spring去扫描包下的文件 -->
<context:component-scan base-
package
=
"com.yq.dao"
></context:component-scan>
<context:component-scan base-
package
=
"com.yq.service"
></context:component-scan>
<!-- 配置数据源 -->
<bean id=
"dataSource"
class
=
"org.apache.commons.dbcp.BasicDataSource"
>
<property name=
"driverClassName"
value=
"com.mysql.jdbc.Driver"
></property>
<property name=
"url"
value=
"jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf-8"
></property>
<property name=
"username"
value=
"root"
></property>
<property name=
"password"
value=
"root"
></property>
</bean>
<bean id=
"jdbcTemplate"
class
=
"org.springframework.jdbc.core.JdbcTemplate"
>
<property name=
"dataSource"
ref=
"dataSource"
></property>
</bean>
</beans>
Dao层
public
class
UserDao {
@Autowired
private
JdbcTemplate jdbcTemplate;
/**
*
* 保存
* com.yq.dao
* 方法名:saveUser
* 创建人:yq
* @param user
* @return boolean
* @exception
* @since 1.0.0
*/
public
boolean
saveUser(User user) {
String sql =
"insert into tz_user(username,password,age,address,email) VALUES(?,?,?,?,?)"
;
int
count = jdbcTemplate.update(sql, user.getUsername(),
user.getPassword(), user.getAge(), user.getAddress(),
user.getEmail());
return
count>
0
?
true
:
false
;
}
}