创建Car类,1) 借助set给属性注入值
2)借助构造方法给属性注入值。【两个都是三个参数的构造方法】
2>创建类,包含集合属性,如何给以下集合属性注入信息
3>创建部门和员工类将类交给spring管理,给dept中的list集合赋值.一种list赋值。util:list
emp:给emp中的dept赋值
4>注解的练习:【熟练掌握】
创建Service以及Dao service依赖dao 。自动注入dao.
dao又多了一个实现类,如何注入?
5>纯注解,不需要配置文件
AOP练习
1>创建普通的类
public class EmpDao(){
//添加方法
//修改的方法
//查看的方法
}
2>创建切面类,切面类中规定对哪些类中的方法做增强
查看,添加,修改 在方法执行都需要获取数据库的链接,在方法执行之后都需要关闭数据库的链接【环绕】
添加,修改的方法,在方法之前执行,需要开启事务,如果没有异常提交事务,如果有异常,需要回滚事务
3>用配置和注解的方式实现
1:构造器注入
set注入:
2:集合注入
ComplexUser类:
package com.cdcas.pojo;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class ComplexUser {
private String uname;
private List<String> hobbyList;
private Map<String,String> residenceMap;
private Set<String> aliasSet;
private String[] array;
/*
* 使用构造方法注入,需要提供带参数的构造方法
* */
public ComplexUser(String uname, List<String> hobbyList, Map<String, String> residenceMap, Set<String> aliasSet, String[] array) {
this.uname = uname;
this.hobbyList = hobbyList;
this.residenceMap = residenceMap;
this.aliasSet = aliasSet;
this.array = array;
}
/*
* 使用属性的setter方法注入,提供默认无参的构造方法,并为注入的属性提供setter方法
* */
public ComplexUser() {
}
public void setUname(String uname) {
this.uname = uname;
}
public void setHobbyList(List<String> hobbyList) {
this.hobbyList = hobbyList;
}
public void setResidenceMap(Map<String, String> residenceMap) {
this.residenceMap = residenceMap;
}
public void setAliasSet(Set<String> aliasSet) {
this.aliasSet = aliasSet;
}
public void setArray(String[] array) {
this.array = array;
}
@Override
public String toString() {
return "ComplexUser{" +
"uname='" + uname + '\'' +
", hobbyList=" + hobbyList +
", residenceMap=" + residenceMap +
", aliasSet=" + aliasSet +
", array=" + Arrays.toString(array) +
'}';
}
}
xml中bean的配置:
<bean id="complexUser" class="com.cdcas.pojo.ComplexUser">
<property name="uname" value="zzx"/>
<property name="hobbyList">
<list>
<value>唱歌value>
<value>跳舞value>
<value>爬山value>
list>
property>
<property name="residenceMap">
<map>
<entry key="dalian" value="大连"/>
<entry key="shanghai" value="上海"/>
<entry key="chengdu" value="成都"/>
map>
property>
<property name="aliasSet">
<set>
<value>zvalue>
<value>zvalue>
<value>xvalue>
set>
property>
<property name="array">
<array>
<value>zvalue>
<value>zvalue>
<value>xvalue>
array>
property>
bean>
package com.cdcas.pojo;
import java.util.List;
public class Dept {
private Integer id;
private List<Emp> empList;
@Override
public String toString() {
return "Dept{" +
"id=" + id +
", emp=" + empList +
'}';
}
public Dept() {
}
public Dept(Integer id, List<Emp>empList) {
this.id = id;
this.empList = empList;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<Emp> getEmpList() {
return empList;
}
public void setEmpList(List<Emp> empList) {
this.empList = empList;
}
}
Emp类:
package com.cdcas.pojo;
public class Emp {
private Integer id;
private String name;
public Emp() {
}
public Emp(Integer id, String name) {
this.id = id;
this.name = 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;
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
xml中bean的配置:
<bean id="emp1" class="com.cdcas.pojo.Emp">
<property name="name" value="zzx"/>
<property name="id" value="1"/>
bean>
<bean id="emp2" class="com.cdcas.pojo.Emp">
<property name="name" value="hw"/>
<property name="id" value="2"/>
bean>
<bean id="dept" class="com.cdcas.pojo.Dept">
<property name="id" value="1"/>
<property name="empList">
<list>
<ref bean="emp1"/>
<ref bean="emp2"/>
list>
property>
bean>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.2.10.RELEASEversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.2.10.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.11version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.7version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.3.22version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.9version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.30version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.4version>
dependency>
dependencies>
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;
//1.定义一个方法获得要管理的对象
// 一个druid的连接池
@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 com.cdcas.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 {
// 连接工厂对象这里的DataSource对象可以直接获取
// 这里可能会出现循环依赖
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
// 给实体类取别名,告诉mybatis实体类在哪里
sqlSessionFactoryBean.setTypeAliasesPackage("com.cdcas.pojo");
return sqlSessionFactoryBean;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mp = new MapperScannerConfigurer();
// 找到mapper
mp.setBasePackage("com.cdcas.mapper");
return mp;
}
}
springConfig:
package com.cdcas.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 com.cdcas.mapper;
import com.cdcas.pojo.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 com.cdcas.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;
}
}