求一个单纯springMVC连接数据库进行增删改查的demospringMVC
解决方案 »
- 求一字符串动态截取、
- --------------->spring+hibernate,事务中有报错却没有回滚<------------------
- struts2配置文件中相关参数?(请高手指教)
- hibernat调用存储过程
- 万分火急!小妹只是一个java的初学者,各位路过的大侠请帮忙看看,小妹在此谢过了!
- 在java中如何调用javascript函数啊?
- struts 中,多个html:submit 提交的问题??请高手指教!
- 请问WEB应用如何打包成安装程序,服务器端最烦的就是JAVA_HOME\CLASSPATH等变量在安装程序中如何自动的设置?
- 谁能帮我解释一下这段程序?#号之间的程序
- Java web使用JDBC连接MySQL进行插入操作时中文显示乱码
- 求大神推荐个学习java中前后天交互的网址
- Spring Aop拦截Action
<?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"
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"> <!-- Scans within the base package of the application for @Components to configure as beans -->
<context:component-scan base-package="org.springframework.docs.test" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <context:property-placeholder location="jdbc.properties"/></beans>第二,获取jdbc模板对象
@Repository
public class JdbcCorporateEventDao implements CorporateEventDao { private JdbcTemplate jdbcTemplate; @Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
} // JDBC-backed implementations of the methods on the CorporateEventDao follow...
}
第三,使用之:
this.jdbcTemplate.update(
"insert into t_actor (first_name, last_name) values (?, ?)",
"Leonor", "Watling");
this.jdbcTemplate.update(
"update t_actor set last_name = ? where id = ?",
"Banjo", 5276L);
this.jdbcTemplate.update(
"delete from actor where id = ?",
Long.valueOf(actorId));
int rowCount = this.jdbcTemplate.queryForObject("select count(*) from t_actor", int.class);List<Actor> actors = this.jdbcTemplate.query(
"select first_name, last_name from t_actor",
new RowMapper<Actor>() {
public Actor mapRow(ResultSet rs, int rowNum) throws SQLException {
Actor actor = new Actor();
actor.setFirstName(rs.getString("first_name"));
actor.setLastName(rs.getString("last_name"));
return actor;
}
});