Spring----02----依赖注入&&CRUD
Spring----02----依赖注入&&CRUD
1. spring依赖注入
依赖注入简单说就是对对象属性赋值,注入方式主要支持两种:
- 构造方法注入
- 调用set方法赋值
1.1 构造方法注入
java:声明各种构造方法
public class User { private Integer id; private String name; private Car car; public User(Integer id, String name, Car car) { System.out.println("public User(Integer id, String name, Car car)!"); this.id = id; this.name = name; this.car = car; } public User() { System.out.println("public User()!"); } public User(Integer id) { System.out.println("public User(Integer id)!"); this.id = id; } public User(String name) { System.out.println("public User(String name)"); this.name = name; } public User(Integer id, String name) { System.out.println("public User(Integer id, String name)!"); this.id = id; this.name = name; } public User(String name,Integer id) { System.out.println("public User(String name,Integer id)!"); this.id = id; this.name = name; } public User(String name,Long id) { System.out.println("public User(String name,Long id)!"); this.id = id.intValue(); this.name = name; } }
xml:调用不同的构造方法,完成赋值
<!-- 调用空参构造方法创建User对象 --> <bean name="user1" class="com.leo.domain.User"></bean> <!-- 调用构造方法public User(Integer id)注入属性值 --> <bean name="user2" class="com.leo.domain.User"> <!-- constructor-arg:使用该标签描述构造方法参数 name:指定构造方法参数名 value:将属性值传入参数 --> <constructor-arg name="id" value="1"/> </bean> <!-- 调用构造方法public User(String name)注入属性值 --> <bean name="user3" class="com.leo.domain.User"> <constructor-arg name="name" value="tom"/> </bean> <!-- public User(Integer id, String name) --> <bean name="user4" class="com.leo.domain.User"> <!-- index:指定参数在参数列表中的索引 --> <constructor-arg name="id" value="2" index="0"/> <constructor-arg name="name" value="jerry"/> </bean> <!-- public User(String name,Integer id) --> <bean name="user5" class="com.leo.domain.User"> <!-- type:指定参数的具体java类型 --> <constructor-arg name="id" value="2" type="java.lang.Integer"/> <constructor-arg name="name" value="jerry" index="0"/> </bean> <!--public User(String name,Long id)--> <bean name="user6" class="com.leo.domain.User"> <constructor-arg name="id" value="2" type="java.lang.Long"/> <constructor-arg name="name" value="jerry" index="0"/> </bean> <bean name="user7" class="com.leo.domain.User"> <constructor-arg name="id" value="2" /> <constructor-arg name="name" value="leo" index="1"/> <constructor-arg name="car" ref="car" /> </bean> <bean name="car" class="com.leo.domain.Car"> <property name="name" value="劳斯莱斯.幻影"/> <property name="color" value="黑色"/> </bean>
1.2 Set方法注入
xml:
<!-- 调用空参构造方法创建User对象 --> <bean name="user1" class="com.leo.domain.User"> <!-- property:属性赋值,调用set方法为属性赋值 name:填写属性名 value:填写属性值 --> <property name="id" value="4"/> <property name="name" value="rose"/> <property name="car" ref="car1"/> </bean> <bean name="car1" class="com.leo.domain.Car"> <property name="name" value="布加迪威航"/> <property name="color" value="粉色"/> </bean> <bean name="car2" class="com.leo.domain.Car"> <property name="name" value="五菱红光"/> <property name="color" value="银白色"/> </bean>
1.3 集合&&数组类型属性注入
xml:
<bean name="cb" class="com.leo.domain.CollectionBean"> <!-- 如果只需要注入一个元素,可以使用value或ref属性直接为list赋值 --> <property name="list" > <!-- 指定list中的三个元素 calue是值类型 ref是对象类型 --> <list> <value>tom</value> <value>jerry</value> <ref bean="car"></ref> </list> </property> <property name="set"> <!-- 指定set中的三个元素 calue是值类型 ref是对象类型 --> <set> <value>jack</value> <value>rose</value> <ref bean="car"></ref> </set> </property> <property name="array"> <!-- 指定array中的三个元素 calue是值类型 ref是对象类型 --> <array> <value>tom</value> <value>jerry</value> <value>jack</value> <value>rose</value> <ref bean="car"></ref> </array> </property> <property name="map"> <map> <!-- 一个entry属性表示一个键值对 key:字符串类型健 key-ref:对象类型键 value:字符串类型值 value-ref:对象类型值 --> <entry key="name" value="tom"/> <entry key="car" value-ref="car"/> <entry key-ref="car" value="tom"/> <entry key-ref="car" value-ref="car"/> </map> </property> <property name="properties"> <props> <!-- 一个prop元素代表Properties中的一个键值对 键值一定是字符串类型 key:填写键 prop标签体:填写值 --> <prop key="driverClass">com.mysql.jdbc.Driver</prop> <prop key="userName">root</prop> <prop key="password">hanjuechen</prop> </props> </property> </bean> <bean name="car" class="com.leo.domain.Car"> <property name="name" value="布加迪威航"/> <property name="color" value="粉色"/> </bean>
2. 配置文件模块化
大型项目中,spring的xml配置文件中可能要配置非常多的对象,都写在一个配置文件中不易于管理,因此我们需要将配置分散到很多配置文件(模块化)中。
在一个“主”配置文件中引入其他模块的配置文件:
<!-- import:引入a.xml与b.xml中的配置(模块化) resource:引入配置文件的路径 --> <import resource="a.xml"></import> <import resource="b.xml"></import>
3. 书写一个案例,完成对数据库的CURD
要求:例子中所涉及到的对象都交给spring容器来管理
3.1 创建实体类
public class User { private Integer id; private String name; 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; } }
3.2 创建库&表
3.3 导入依赖
父工程:
<dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.6.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> </dependencies>
子工程:
<dependencies> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency> <!-- dbutils --> <dependency> <groupId>commons-dbutils</groupId> <artifactId>commons-dbutils</artifactId> <version>1.7</version> </dependency> <!-- c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.5</version> </dependency> </dependencies>
3.4 分析依赖关系:
3.5 连接池配置:
<!-- Spring容器管理连接池 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring?serverTimezone=UTC"/> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="user" value="root"/> <property name="password" value="hanjuechen"/> </bean>
3.6 QuerryRunner对象配置
<bean name="queryRunner" class="org.apache.commons.dbutils.QueryRunner"> <constructor-arg name="ds" ref="dataSource"/> </bean>
3.7 配置并书写Dao对象
Dao接口:
public interface UserDao { void save(User user); }
Dao实现类:
public class UserDaoImpl implements UserDao{ private QueryRunner queryRunner; public void save(User user) { //1 书写sql String sql = "isert into t_user values(?,?)"; //2 执行sql try { queryRunner.update(sql, user.getName()); } catch (SQLException throwables) { throwables.printStackTrace(); } } //创建set方法,用于set方法注入 public void setQueryRunner(QueryRunner queryRunner) { this.queryRunner = queryRunner; } }
xml配置:
<!-- UserDao对象 --> <bean name="userDao" class="com.leo.dao.UserDaoImpl"> <property name="queryRunner" ref="queryRunner"/> </bean>
3.8 书写UserService对象
接口:
public interface UserService { //注册用户(保存) void regist(User user); }
实现类:
public class UserServiceImpl implements UserService{ private UserDao userDao; public void regist(User user) { userDao.save(user); } public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
xml配置:
<!-- UserDervice对象 --> <bean name="userService" class="com.leo.service.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean>
3.9 测试
@Test public void testSave(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) ac.getBean("userService"); User u = new User(); u.setId(1); u.setName("tom"); userService.regist(u); }