创建Car类,1) 借助set给属性注入值
<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">
<bean id="car" class="openlab.com.beans.Car">
<constructor-arg value="baoma" index="0">constructor-arg>
<constructor-arg value="german" index="1">constructor-arg>
<constructor-arg value="100" index="2">constructor-arg>
bean>
beans>
2)借助构造方法给属性注入值。【两个都是三个参数的构造方法】
<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">
<bean id="car" class="openlab.com.beans.Car">
<property name="brand" value="baoma">property>
<property name="corp" value="german">property>
<property name="price" value="400000">property>
bean>
beans>
2>创建类,包含集合属性,如何给以下集合属性注入信息
CollectionTest类
package openlab.com.beans;
import lombok.Data;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Data
public class CollectionTest {
private String[] arr;
private List<String> list;
private Map<String,String> map;
private Set<String> set;
private List<Emp> emps;
private Map<String,Emp> empMap;
@Override
public String toString(){
return "CollectionTest{" +"arr"+ Arrays.toString(arr)+
", list"+list+
", map"+map+
", set"+set+
", emps"+emps+
", empMap"+empMap+"}";
}
}
bean中配置
<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">
<bean id="CollectionTest" class="openlab.com.beans.CollectionTest">
<property name="arr" >
<array>
<value>1value>
<value>1value>
<value>1value>
<value>1value>
array>
property>
<property name="list">
<list>
<value>1value>
<value>1value>
<value>1value>
list>
property>
<property name="map">
<map>
<entry key="1" value="c">entry>
<entry key="2" value="y">entry>
<entry key="3" value="x">entry>
map>
property>
<property name="set">
<set>
<value>1value>
<value>1value>
<value>1value>
set>
property>
<property name="emps">
<list>
<value>1value>
<value>1value>
<value>1value>
list>
property>
<property name="empMap">
<map>
<entry key="1" value="c">entry>
<entry key="2" value="y">entry>
<entry key="3" value="x">entry>
map>
property>
bean>
beans>
3>创建部门和员工类将类交给spring管理,给dept中的list集合赋值.一种list赋值。util:list
emp:给emp中的dept赋值
<bean id="emp1" class="openlab.com.Emp">
<property name="name" value="cyx"/>
<property name="id" value="1"/>
bean>
<bean id="emp2" class="openlab.com.Emp">
<property name="name" value="cyx2"/>
<property name="id" value="2"/>
bean>
<bean id="dept" class="openlab.com.Dept">
<property name="id" value="1"/>
<property name="empList">
<list>
<ref bean="emp1"/>
<ref bean="emp2"/>
list>
property>
bean>
AOP练习
1>创建普通的类
public class EmpDao(){
//添加方法
//修改的方法
//查看的方法
}
2>创建切面类,切面类中规定对哪些类中的方法做增强
查看,添加,修改 在方法执行都需要获取数据库的链接,在方法执行之后都需要关闭数据库的链接【环绕】
添加,修改的方法,在方法之前执行,需要开启事务,如果没有异常提交事务,如果有异常,需要回滚事务
3>用配置和注解的方式实现
jdbcConfig
import javax.sql.DataSource;
public class jdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.user}")
private String userName;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource ds=new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(userName);
ds.setPassword(password);
return ds;
}
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
return transactionManager;
}
}
MybatisConfig:
package openlab.com.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
public class MybatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage("com.cdcas.pojo");
return sqlSessionFactoryBean;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mp = new MapperScannerConfigurer();
mp.setBasePackage("com.cdcas.mapper");
return mp;
}
}
springConfig:
package openlab.com.config;
import org.springframework.context.annotation.*;
//Configuration标识这个类为一个配置类
@Configuration
//扫描我们的包
@ComponentScan("com.cdcas")
//导入要加载的配置类
@Import({jdbcConfig.class,MybatisConfig.class})
//导入要加载的配置文件
@PropertySource("classpath:jdbc.properties")
//告诉Spring开启了Aop注解
@EnableAspectJAutoProxy
//告诉Spring用注解事务驱动
@EnableTransactionManagement
public class SpringConfig {
}
EmpMapper:
package openlab.com.mapper;
import openlab.com.beans.Emp;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface EmpMapper {
// 添加
@Insert("insert into tbl_emp values (null,#{name})")
void add(Emp emp);
// 修改
@Update("update tbl_emp set name=#{name} where id=#{id}")
void update(Emp emp);
// 查看
@Select("select * from tbl_emp")
List<Emp> select();
}
AOP:
package openlab.com.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.sql.Connection;
@Component
@Aspect
public class ProjectAdvice {
@Pointcut("execution(* com.cdcas.service.*Service.*(..))")
private static void servicePt() {
}
@Pointcut("execution(* com.cdcas.service.*Service.*(..))&&!" +
"execution(* com.cdcas.service.*Service.select(..))")
private static void servicePt2() {
}
@Autowired
DataSource dataSource;
@Around("servicePt()")
public Object operation(ProceedingJoinPoint pjp) throws Throwable {
Connection connection = dataSource.getConnection();
System.out.println("数据库连接" + connection);
Object obj = pjp.proceed();
System.out.println("关闭数据库连接");
connection.close();
return obj;
}
@Around("servicePt2()")
public Object operation2(ProceedingJoinPoint pjp) {
try {
return pjp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
}