1. spring核心配置文件:
<?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"> <!-- 读取占位符配置文件,和mybatis的一样 --> <context:property-placeholder location="classpath:db.properties" /> <!-- apache的连接池依赖类,pom里配置引入commons-dbcp --> <!-- 这里需要占位符配置url、username、password --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db.driver}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> </bean> <!-- spring提供的jdbc template方便dao层编写,pom引入依赖spring-jdbc --> <!-- 这里需要注入上面生成好的数据库连接池dataSource --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 自己编写的持久层,内置一个叫template的属性(必须要有setter) --> <!-- 往属性template注入上面生成好的jdbcTemplate --> <bean id="userDao" class="dao.daoXmlImpl"> <property name="template" ref="jdbcTemplate"></property> </bean> <!-- 自己编写的业务层,内置一个叫dao的属性(必须要有setter) --> <!-- 往属性dao注入上面生成好的userDao --> <bean id="userService" class="service.xmlImpl"> <property name="dao" ref="userDao"></property> </bean> </beans>
2.POJO类
public class User { private int id; private String username; @Override public String toString() { return "User [id=" + id + ", username=" + username + ", address=" + address + "]"; } private String address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
3. dao类
public class daoXmlImpl implements userDao { private JdbcTemplate template; // 留一个dao的setter让Spring帮忙注入jdbcTemplate public void setTemplate(JdbcTemplate template) { this.template = template; } @Override public User getUserById(int id) { // 业务SQL String sql = "SELECT * FROM user WHERE id = ?"; // 参数数组 Object[] args = { id }; // 使用jdbcTemplate来查询数据库 return template.queryForObject(sql, args, new RowMapper<User>() { @Override public User mapRow(ResultSet rs, int rowNum) throws SQLException { User user = new User(); user.setId(rs.getInt("id")); user.setUsername(rs.getString("username")); user.setAddress(rs.getString("address")); return user; } }); } }
4.service类
public class xmlImpl implements userService{ private userDao dao; // 留一个dao的setter让Spring帮忙注入userDao public void setDao(userDao dao) { this.dao = dao; } @Override public User findUser(int id) { return dao.getUserById(id); } }
5.使用类(这里用servlet)
public class userView extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取参数 String id = request.getParameter("id"); // 获取Spring IOC容器 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 通过Spring IOC容器获取 service bean userService service = context.getBean(userService.class); // 调用service的方法,获取user User user = service.findUser(Integer.parseInt(id)); // 响应,打印user response.setContentType("application/json;charset=utf-8"); response.getWriter().print(user); } }