taglibs
standard
1.1.2
Tips: 字符编码的Filter配置要在支持REST风格的filter配置之前
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:SpringMVC.xml
1
DispatcherServlet
/
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
CharacterEncodingFilter
/*
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
/*
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:context=“http://www.springframework.org/schema/context”
xmlns:mvc=“http://www.springframework.org/schema/mvc”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
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
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<context:component-scan base-package=“com”/>
======================================================================
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/tx
jdbc.username=root
jdbc.password=xxxxxxx
2.在Spring容器中引入pro配置文件,配置数据源,配置JDBCTemplate对象
SpringMVC.xml
<context:property-placeholder location=“classpath:jdbc.properties”/>
===================================================================
EmployeeDao类:
@Repository
public class EmployeeDao {
@Autowired //按照类型注入
JdbcTemplate jdbcTemplate;
//保存员工信息到数据库
public void save( Employee employee)
{
String sql=“insert employee values(?,?,?,?,?)”;
jdbcTemplate.update(sql,employee.getId(),employee.getName(),employee.getDepartment(),
employee.getEamil(),employee.getGender());
}
//查询并返回对应员工信息
public Employee exist(Employee employee)
{
String sql=“select * from employee where name=? and id= ?”;
try{
Employee employee1 = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(Employee.class), employee.getName(), employee.getId());
return employee1;
}
catch (Exception e)
{
return null;
}
}
//删除某个员工的信息
public void delete(Employee employee)
{
String sql=“delete from employee where id=?”;
jdbcTemplate.update(sql,employee.getId());
}
//返回所有员工的信息
public List getAllEmployees()
{
String sql=“select* from employee”;
List list = jdbcTemplate.query(sql, new BeanPropertyRowMapper(Employee.class));
return list;
}
}
==========================================================================
employee类:
@Component
public class Employee {
private Integer id;
private String name;
private String department;
private String eamil;
private Integer gender;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getEamil() {
return eamil;
}
public void setEamil(String eamil) {
this.eamil = eamil;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
@Override
public String toString() {
return “Employee{” +
“id=” + id +
“, name=’” + name + ‘’’ +
“, department=’” + department + ‘’’ +
“, eamil=’” + eamil + ‘’’ +
“, gender=”