解释:
JdbcTemplate 封装常用的JDBC方法
HibernateTemplate 封装常用Hibernate方法
JdbcDaoSupport JDBC数据访问对象的基类
HibernateDaoSupport -Hibernate数据访问对象的基类
JdbcDaoSupport是利用JDBC技术编写的Dao的父类,通过该类提供的方法,可便于获取Connection和JdbcTemplate等对象信息。
JdbcDaoSupport使用时 需要注入DataSource对象
JdbcDaoSupport使用时 对代码有一定的侵入性
代码:
db.properties 配置:
driverName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
username=root
password=root
maxActive=5
maxWait =5000
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:util="http://www.springframework.org/schema/util"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 加载数据库链接配置 -->
<util:properties id="jdbc" location="classpath:db.properties"></util:properties>
<!-- 配置数据源 数据库链接池 -->
<bean id="db" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 设置JDBC连接URL -->
<property name="url" value="#{jdbc.url}" />
<!-- 设置JDBC驱动名称 -->
<property name="driverClassName" value="#{jdbc.driverName}" />
<!-- 设置数据库用户名 -->
<property name="username" value="#{jdbc.username}" />
<!-- 设置数据库密码 -->
<property name="password" value="#{jdbc.password}" />
<!-- 设置数据库密码 -->
<property name="maxActive" value="#{jdbc.maxActive}" />
<!-- 设置数据库密码 -->
<property name="maxWait" value="#{jdbc.maxWait}" />
</bean>
<!-- Spring对jdbc的操作 ref与数据源 id相对应-->
<bean id="jdbcfz" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="db"></property>
</bean>
<!--开启 注解的包扫描 -->
<context:component-scan base-package="com.wf"></context:component-scan>
<!--配置我们的mvc驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置 视图解析器 读取根目录所有的jsp ,需要使用的spring-webmvc-3.2.8.RELEASE.jar 机制使用的是servlet的转发和重定向 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
UserInfoController配置:
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.wf.dao.UserDao;
import com.wf.entity.UserInfo;
@Controller
public class UserInfoController {
@Resource
private UserDao dao;
@RequestMapping("tolist.do")
public String toList(ModelMap mop){
List<UserInfo> list = dao.findList();
mop.put("list", list);
return "list";
}
@RequestMapping("tosaveOrupdate.do")
public String saveOrupdate(UserInfo u){
if(u!=null&&u.getId()!=null&&u.getId()!=""&&u.getId().length()>0){
dao.update(u);
}else{
u.setId(String.valueOf(System.currentTimeMillis()).substring(9,13));
dao.save(u);
}
return "redirect:tolist.do";
}
@RequestMapping("toupdate.do")
public String tosave(String id,ModelMap map){
if(id !=null){
UserInfo u = dao.findId(id);
map.put("user", u);
}
return "saveOrupdate";
}
@RequestMapping("todel.do")
public String del(UserInfo u){
dao.del(u.getId());
return "redirect:tolist.do";
}
}
dao :
import java.util.List;
import com.wf.entity.UserInfo;
public interface UserDao {
public UserInfo findId(String id);
public List<UserInfo> findList();
public void save(UserInfo u);
public void update(UserInfo u);
public void del(String id);
}
daoimpl:
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.wf.entity.UserInfo;
@Repository("userDao")
public class UserDaoImpl implements UserDao{
@Resource
private JdbcTemplate jdbctemp;
public UserInfo findId(String id) {
String sql =" select * from userinfo where id=?";
return jdbctemp.queryForObject(sql, new Object[]{id},new UserRowMapper());
}
public List<UserInfo> findList() {
String sql =" select * from userinfo";
List<UserInfo> list =jdbctemp.query(sql, new UserRowMapper());
return list;
}
public void save(UserInfo u) {
String sql ="insert into userinfo values(?,?,?)";
Object[] param ={u.getId(),u.getUserName(),u.getPassWord()};
jdbctemp.update(sql,param);
}
public void update(UserInfo u) {
String sql ="update userinfo set userName=?,passWord=? where id=?";
Object[] param ={u.getUserName(),u.getPassWord(),u.getId()};
jdbctemp.update(sql,param);
}
public void del(String id) {
String sql ="delete from userinfo where id=?";
jdbctemp.update(sql,id);
}
class UserRowMapper implements RowMapper<UserInfo>{
public UserInfo mapRow(ResultSet rs,int index) throws SQLException{
UserInfo u = new UserInfo();
u.setId(rs.getString("id"));
u.setPassWord(rs.getString("passWord"));
u.setUserName(rs.getString("userName"));
return u;
}
}
}
实体类:
package com.wf.entity;
import java.io.Serializable;
import org.springframework.stereotype.Component;
@Component
public class UserInfo implements Serializable{
private static final long serialVersionUID = 1L;
private String id;
private String userName;
private String passWord;
public UserInfo() {
}
public UserInfo(String id, String userName, String passWord) {
this.id = id;
this.userName = userName;
this.passWord = passWord;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
测试类:
import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wf.dao.UserDao;
import com.wf.entity.UserInfo;
public class TestDemo {
@Test
public void test(){
ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");//实例化容器
UserDao user =ac.getBean("userDao",UserDao.class);
// UserInfo u = new UserInfo("11", "22", "33");
// user.save(u);
List<UserInfo> list=user.findList();
for (UserInfo i:list) {
System.out.println(i.getUserName());
}
}
}
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 使用Filter解决中文乱码问题 post请求-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
jsp页面:
新增和修改页面
<form action="tosaveOrupdate.do" method="post">
<input type="hidden" id="id" name="id" value="${user.id}"><br>
userName:<input type="text" id="userName" name="userName" value="${user.userName}"><br>
passWord:<input type="text" id="passWord" name="passWord" value="${user.passWord}"><br>
<input type="submit" value="提交"><br>
</form>
list页面:
<table>
<tr><a href="toupdate.do">添加</a></tr>
<tr>
<td>id</td>
<td>名字</td>
<td>密码</td>
</tr>
<c:forEach items="${list }" var="u">
<tr>
<td>${u.id }</td>
<td>${u.userName }</td>
<td>${u.passWord }</td>
<td><a href="toupdate.do?id=${u.id}">修改</a><a href="todel.do?id=${u.id}">删除</a></td>
</tr>
</c:forEach>
</table>