JavaEE学习日志(八十八)
- Spring
- 依赖注入
- 构造方法注入
- set方法注入属性(常用)
- p名称空间注入
- 注入集合属性
- springIOC完成账户表的CRUD
Spring
依赖注入
构造方法注入
有一个User类
package com.itheima.domain;
import java.util.Date;
public class User {
private Integer id;
private String username;
private Date birthday;
private Character sex;
public User(Integer id, String username) {
this.id = id;
this.username = username;
}
public User() {
}
public Integer getId() {
return id;
}
public String getUsername() {
return username;
}
public Date getBirthday() {
return birthday;
}
public Character getSex() {
return sex;
}
}
三种构造方法的注入方式:索引,参数类型和参数名称
注意:使用索引方式注入时,如果有多个构造方法,可能会出现错误
<!--依赖注入-->
<!--默认创建对象的方式,使用默认的空参构造创建对象-->
<bean id="user" class="com.itheima.domain.User">
<!--constructor-arg通过构造方法的索引赋值-->
<!-- <constructor-arg index="0" value="1"></constructor-arg>-->
<!-- <constructor-arg index="1" value="张三"></constructor-arg>-->
<!--通过参数类型赋值-->
<!-- <constructor-arg type="java.lang.Integer" value="2"></constructor-arg>-->
<!-- <constructor-arg type="java.lang.String" value="李四"></constructor-arg>-->
<!--通过构造方法参数的名字赋值-->
<constructor-arg name="id" value="3"></constructor-arg>
<constructor-arg name="username" value="王五"></constructor-arg>
</bean>
测试类
@Test
public void test3(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = ac.getBean("user", User.class);
System.out.println(user.getId());
System.out.println(user.getUsername());
}
补充:
-
value
属性:只能赋值简单类型 -
ref
属性:pojo类型,复杂类型,关联创建好的对象
如Date类型
<bean id="user" class="com.itheima.domain.User">
<!--constructor-arg通过构造方法的索引赋值-->
<!-- <constructor-arg index="0" value="1"></constructor-arg>-->
<!-- <constructor-arg index="1" value="张三"></constructor-arg>-->
<!--通过参数类型赋值-->
<!-- <constructor-arg type="java.lang.Integer" value="2"></constructor-arg>-->
<!-- <constructor-arg type="java.lang.String" value="李四"></constructor-arg>-->
<!--通过构造方法参数的名字赋值-->
<constructor-arg name="id" value="3"></constructor-arg>
<constructor-arg name="username" value="王五"></constructor-arg>
<constructor-arg name="sex" value="男"></constructor-arg>
<constructor-arg name="birthday" ref="birthday"></constructor-arg>
</bean>
<bean id="birthday" class="java.util.Date"></bean>
set方法注入属性(常用)
property
:属性注入
<bean id="birthday" class="java.util.Date"></bean>
<!--通过set方法注入-->
<bean id="user2" class="com.itheima.domain.User">
<!--property:属性注入-->
<property name="username" value="abc"></property>
<property name="birthday" ref="birthday"></property>
<property name="sex" value="男"></property>
<property name="id" value="4"></property>
</bean>
p名称空间注入
原理也是基于set方法,但无法注入集合
1、在头部中引入p名称空间:第三行
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
2、使用p名称空间注入
<!--使用p名称空间注入-->
<bean id="user3" class="com.itheima.domain.User"
p:id="5" p:username="abc" p:sex="男" p:birthday-ref="birthday"
>
</bean>
注入集合属性
一、User中添加集合属性list,set和数组
private List<String> list;
private Set<String> set;
private String[] strs;
注意:array,list,set结构相同,标签可以混用
<!--注入集合属性-->
<bean id="user4" class="com.itheima.domain.User">
<!--array,list,set结构相同,标签可以混用-->
<property name="list">
<list>
<value>abc</value>
<value>abcd</value>
<value>abcde</value>
</list>
</property>
<property name="set">
<set>
<value>222</value>
<value>333</value>
<value>444</value>
</set>
</property>
<property name="strs">
<array>
<value>555</value>
<value>666</value>
<value>777</value>
</array>
</property>
</bean>
二、User中添加map和properties
private Map<String,String> map;
private Properties properties;
注意:map和properties结构相同,可以混用
<!--map和properties结构相同,可以混用-->
<bean id="user5" class="com.itheima.domain.User">
<property name="map">
<map>
<!--键值对配置-->
<entry key="1" value="aaa"></entry>
<entry key="2" value="bbb"></entry>
<entry key="3" value="ccc"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="五">ddd</prop>
<prop key="六">eee</prop>
</props>
</property>
</bean>
springIOC完成账户表的CRUD
创建数据库表
create table account(id int primary key auto_increment,name varchar(40),money float) character set utf8 collate utf8_general_ci;insert into account(name,money) values('aaa',1000);insert into account(name,money) values('bbb',1000);insert into account(name,money) values('ccc',1000);
AccountDaoImpl
package com.itheima.dao.impl;
import com.itheima.dao.AccountDao;
import com.itheima.domain.Account;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.util.List;
public class AccountDaoImpl implements AccountDao {
QueryRunner qr;
public void setQr(QueryRunner qr) {
this.qr = qr;
}
@Override
public List<Account> findAll() {
String sql = "select * from account";
try {
List<Account> accountList = qr.query(sql, new BeanListHandler<Account>(Account.class));
return accountList;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public Account findById(Integer id) {
String sql = "select * from account where id = ?";
try {
return qr.query(sql,new BeanHandler<Account>(Account.class),id);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public void save(Account account) {
String sql = "insert into account values(null,?,?)";
try {
qr.update(sql,account.getName(),account.getMoney());
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void update(Account account) {
String sql = "update account set name = ? , money = ? where id =?";
try {
qr.update(sql,account.getName(),account.getMoney(),account.getId());
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void del(Integer id) {
String sql = "delete from account where id = ?";
try {
qr.update(sql, id);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
AccountServiceImpl
package com.itheima.service.impl;
import com.itheima.dao.AccountDao;
import com.itheima.dao.impl.AccountDaoImpl;
import com.itheima.domain.Account;
import com.itheima.service.AccountService;
import java.util.List;
public class AccountServiceImpl implements AccountService {
//创建容器,使用依赖注入
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public List<Account> findAll() {
return accountDao.findAll();
}
@Override
public Account findById(Integer id) {
return accountDao.findById(id);
}
@Override
public void save(Account account) {
accountDao.save(account);
}
@Override
public void update(Account account) {
accountDao.update(account);
}
@Override
public void del(Integer id) {
accountDao.del(id);
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!--创建AccountService对象:需要AccountDao对象,依赖注入dao对象-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--创建AccountDao对象,需要qr-->
<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
<!--通过set方法注入qr-->
<property name="qr" ref="qr"></property>
</bean>
<!--创建qr对象,需要数据源对象,通过构造方法的参数类型注入-->
<bean id="qr" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg type="javax.sql.DataSource" ref="dataSource"></constructor-arg>
</bean>
<!--创建数据源对象:需要注入四个参数,通过set方法注入-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
测试类:只测试一个findAll
@Test
public void test(){
//创建容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//创建service对象
AccountService accountService = ac.getBean("accountService",AccountService.class);
List<Account> accountList = accountService.findAll();
for (Account account : accountList) {
System.out.println(account);
}
}