一、在 web 环境中使用 spring 容器
1. 导包
2. 在 web.xml 中配置 listener => ContextLoaderListener
配置参数,指定 spring 配置路径
applicationContext.xml 配置示例

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> 3 4 5 <!-- 配置Dao --> 6 <bean name="customerDao" class="cn.itheima.dao.impl.CustomerDaoImpl" ></bean> 7 <bean name="linkManDao" class="cn.itheima.dao.impl.LinkManDaoImpl" ></bean> 8 <bean name="userDao" class="cn.itheima.dao.impl.UserDaoImpl" ></bean> 9 <!-- 配置Service --> 10 <bean name="customerService" class="cn.itheima.service.impl.CustomerServiceImpl" > 11 <property name="customerDao" ref="customerDao" ></property> 12 </bean> 13 <bean name="linkManService" class="cn.itheima.service.impl.LinkManServiceImpl" > 14 <property name="cd" ref="customerDao" ></property> 15 <property name="lmd" ref="linkManDao" ></property> 16 </bean> 17 <bean name="userService" class="cn.itheima.service.impl.UserServiceImpl" > 18 <property name="ud" ref="userDao" ></property> 19 </bean> 20 21 22 </beans>
3. 在 Action 中,使用工具类获取容器
1. web.xml 中添加 spring 容器的监听器
<!-- 可以让spring容器随项目的启动而创建,随项目的关闭而销毁 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定加载spring配置文件的位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
2. 在 Action 中获得 spring 容器 => 从application域中获取
(1)获得 servletContext 对象
ServletContext sc = ServletActionContext.getServletContext();
(2)从 sc 中获取 ac 容器
WebbApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext ( ServletContext sc ) ;
(3)从容器中获取 CustomerService
CustomerService cs = (CustomerService) ac.getBean("customerService");
二、aop 实现
1. 导包:在(一)的基础包上添加以下四个阴影部分包
2. 准备目标对象
3. 准备通知
4. 配置将通知织入目标对象
三、spring 整合 jdbc
spring 中提供了一个可以操作数据库的对象,对象封装了 jdbc 技术
1. 准备工作
1. 导包
2. 准备数据库: 库 -- mybatis 表 -- t-user 列如下 其中id自增
3.测试连接:新建类 junit 运行后表中成功插入

1 package com.dic.jdbc; 2 3 import java.beans.PropertyVetoException; 4 5 import org.junit.Test; 6 import org.springframework.jdbc.core.JdbcTemplate; 7 8 import com.mchange.v2.c3p0.ComboPooledDataSource; 9 10 11 public class JDBC { 12 13 @Test 14 public void fun1() throws PropertyVetoException{ 15 16 // 1.准备连接池 17 ComboPooledDataSource dataSource = new ComboPooledDataSource(); 18 dataSource.setDriverClass("com.mysql.jdbc.Driver"); 19 dataSource.setJdbcUrl("jdbc:mysql:///mybatis"); 20 dataSource.setUser("root"); 21 dataSource.setPassword("root"); 22 23 24 // 2.创建JDBC模板 25 JdbcTemplate jt = new JdbcTemplate(); 26 jt.setDataSource(dataSource); 27 28 // 3.书写sql,并执行 29 String sql = "insert into user values(null,'rose',null,null,null) "; 30 jt.update(sql); 31 32 } 33 34 }
2. 用 spring 容器管理
1. 建立 bean 对应数据库字段

1 package com.dic.bean; 2 3 public class User { 4 5 private Integer id; 6 private String name; 7 public Integer getId() { 8 return id; 9 } 10 public void setId(Integer id) { 11 this.id = id; 12 } 13 public String getName() { 14 return name; 15 } 16 public void setName(String name) { 17 this.name = name; 18 } 19 @Override 20 public String toString() { 21 return "User [id=" + id + ", name=" + name + "]"; 22 } 23 24 }
2. 建立操作接口

1 package com.dic.jdbc; 2 3 import java.util.List; 4 5 import com.dic.bean.User; 6 7 public interface UserDao { 8 9 //增 10 void save(User u); 11 //查 12 User getById(Integer id); 13 //查 14 int getTotalCount(); 15 //查 16 List<User> getAll(); 17 18 //删 19 void delete(Integer id); 20 //改 21 void update(User u); 22 }
3. 建立操作接口的实现类

1 package com.dic.jdbc; 2 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.util.List; 6 7 import org.springframework.jdbc.core.JdbcTemplate; 8 import org.springframework.jdbc.core.RowMapper; 9 10 import com.dic.bean.User; 11 12 13 14 //使用JDBC模板实现增删改查 15 public class UserDaoImpl implements UserDao { 16 17 private JdbcTemplate jt; 18 19 @Override 20 public void save(User u) { 21 String sql = "insert into t_user values(null,?) "; 22 jt.update(sql, u.getName()); 23 } 24 25 @Override 26 public void delete(Integer id) { 27 String sql = "delete from t_user where id = ? "; 28 jt.update(sql, id); 29 } 30 @Override 31 public void update(User u) { 32 String sql = "update t_user set name = ? where id=? "; 33 jt.update(sql,u.getId(), u.getName()); 34 } 35 @Override 36 public User getById(Integer id) { 37 String sql = "select * from t_user where id = ? "; 38 return jt.queryForObject(sql,new RowMapper<User>(){ 39 @Override 40 public User mapRow(ResultSet rs, int arg1) throws SQLException { 41 User u = new User(); 42 u.setId(rs.getInt("id")); 43 u.setName(rs.getString("name")); 44 return u; 45 }}, id); 46 47 } 48 @Override 49 public int getTotalCount() { 50 String sql = "select count(*) from t_user "; 51 Integer count = jt.queryForObject(sql, Integer.class); 52 return count; 53 } 54 55 @Override 56 public List<User> getAll() { 57 String sql = "select * from t_user "; 58 List<User> list = jt.query(sql, new RowMapper<User>(){ 59 @Override 60 public User mapRow(ResultSet rs, int arg1) throws SQLException { 61 User u = new User(); 62 u.setId(rs.getInt("id")); 63 u.setName(rs.getString("name")); 64 return u; 65 }}); 66 return list; 67 } 68 69 }