一、在 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 }