B站搜索 狂神说JAVA
1.DAO层
全称为data access object,为了建立一个健壮的J2EE应用,应该将所有对数据源的访问操作抽象封装在一个公共API中。用程序设计的语言来说,就是建立一个接口,接口中定义了此应用程序中将会用到的所有事务方法。在这个应用程序中,当需要和数据源进行交互的时候则使用这个接口,并且编写一个单独的类来实现这个接口在逻辑上对应这个特定的数据存储。
2.service层
service层叫服务层,被称为服务,肯定是相比之下比较高层次的一层结构,相当于将几种操作封装起来。用户只和service交互,而DAO层对用户不可见。同时减少了耦合。
3.IDEA中建立过程
1.建立maven项目,
2.导入依赖,刷新maven
3.删除src文件,新建module,在java中新建 com.name.UserDao 和接口实现类UserDaoImpl
4.新建service层, 新建com.name.service,建立Userservice类接口实现类UserserviceImpl接口实现类
5.新建test进行测试。
4.代码及结构01
//UserDao
package com.li.dao;
public interface UserDao {
void getUser();
}
//实现类
package com.li.dao;
public class UserDaoImpl implements UserDao
{
public void getUser()
{
System.out.println("获取用户数据");
}
}
//service
package com.li.service;
public interface UserService {
void getUser();
}
//service实现类
package com.li.service;
import com.li.dao.UserDao;
import com.li.dao.UserDaoImpl;
public class UserServiceImpl implements UserService
{
private UserDao userDao=new UserDao();
public void getUser()
{
userDao.getUser();
}
}
缺点:上述方式改动太多,较为繁琐,耦合性还是太强
5.利用IOC进行改进
程序主动创建对象,用set注入之后,程序不再具有主动性,变成了被动的接受对象。从本质上解决问题,系统耦合性降低,专注业务实现,。实现控制反转
//修改service实现类如下
package com.li.service;
import com.li.dao.UserDao;
import com.li.dao.UserDaoImpl;
import com.li.dao.UserDaoMysql;
public class UserServiceImpl implements UserService
{
private UserDao userDao;
public void setUserDao(UserDao userDao)
{
this.userDao=userDao;
}
public void getUser()
{
userDao.getUser();
}
}
import com.li.dao.UserDaoImpl;
import com.li.dao.UserDaoMysql;
import com.li.service.UserService;
import com.li.service.UserServiceImpl;
public class test
{
public static void main(String[] args) {
UserService userService=new UserServiceImpl();
((UserServiceImpl)userService).setUserDao(new UserDaoMysql());
userService.getUser();
}
}
这是IoC(控制反转)的原型,反转(理解):主动权交给了用户
6.利用Spring进行修改
写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" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<!--相当于new一个对象-->
<bean id="DaoImpl" class="com.li.dao.UserDaoImpl"/>
<bean id="MysqlImpl" class="com.li.dao.UserDaoMysql"/>
<!--相当于new一个对象 ref表明要引用某一个对象-->
<bean id="UserServiceImpl" class="com.li.service.UserServiceImpl">
<property name="userDao" ref="DaoImpl"/>
</bean>
</beans>
修改test IOC默认使用无参构造,可以使用无参构造
public class test
{
public static void main(String[] args) {
//获取上下文
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserServiceImpl userService= (UserServiceImpl)context.getBean("UserServiceImpl");
userService.getUser();
}
}
6.Spring配置
1Bean配置
<!--id:bean的唯一标识符,也就是相当于我们学的对象名
class:bean对象所对应的会限定名:包名+类型
name:也是别名,而且name可以同时取多个别名 -->
<bean id="user" class="pojo.User" name="u1 u2,u3;u4">
<property name="name" value="iii"/>
</bean>
<!-- 使用时
User user2 = (User) context.getBean("u1");
-->
2 import
import一般用于团队开发使用,它可以将多个配置文件,导入合并为一个
用import将所有人的beans.xml合并为一个总的!
- 张三(beans.xm1)
- 李四(beans2.xm1)
- 王五(beans3.xm1)
- applicationContext.xml
```xml
<import resource="beans.xm1"/>
<import resource="beans2.xml"/>
<import resource="beans3.xm1"/>
```
7.依赖注入
7.1 set注入
student类
package com.li.pojo;
import java.util.*;
public class student {
//别忘了写get和set方法(用lombok注解也行)
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String, String> card;
private Set<String> game;
private Properties infor;
private String wife;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGame() {
return game;
}
public void setGame(Set<String> game) {
this.game = game;
}
public Properties getInfor() {
return infor;
}
public void setInfor(Properties infor) {
this.infor = infor;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
@Override
public String toString() {
return "Student{" +"\n"+
"name='" + name + '\'' +"\n"+
", address=" + address.toString() +"\n"+
", books=" + Arrays.toString(books) +"\n"+
", hobbies=" + hobbies +"\n"+
", card=" + card +"\n"+
", game=" + game +"\n"+
", infor=" + infor +"\n"+
", wife='" + wife + '\'' +"\n"+
'}';
}
}
Address类
package com.li.pojo;
import java.util.*;
public class student {
//别忘了写get和set方法(用lombok注解也行)
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String, String> card;
private Set<String> game;
private Properties infor;
private String wife;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGame() {
return game;
}
public void setGame(Set<String> game) {
this.game = game;
}
public Properties getInfor() {
return infor;
}
public void setInfor(Properties infor) {
this.infor = infor;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
@Override
public String toString() {
return "Student{" +"\n"+
"name='" + name + '\'' +"\n"+
", address=" + address.toString() +"\n"+
", books=" + Arrays.toString(books) +"\n"+
", hobbies=" + hobbies +"\n"+
", card=" + card +"\n"+
", game=" + game +"\n"+
", infor=" + infor +"\n"+
", wife='" + wife + '\'' +"\n"+
'}';
}
}
Beans.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">
<bean id="address" class="com.li.pojo.Address">
<property name="address" value="address你好" />
</bean>
<bean id="Student" class="com.li.pojo.student">
<!--第一种,普通值注入 -->
<property name="name" value="name你好" />
<!--第二种,ref注入 -->
<property name="address" ref="address" />
<!--数组注入 -->
<property name="books">
<array>
<value>三国</value>
<value>西游</value>
<value>水浒</value>
</array>
</property>
<!--list列表注入 -->
<property name="hobbies">
<list>
<value>唱</value>
<value>跳</value>
<value>rap</value>
<value>篮球</value>
</list>
</property>
<!--map键值对注入 -->
<property name="card">
<map>
<entry key="username" value="root" />
<entry key="password" value="root" />
</map>
</property>
<!--set(可去重)注入 -->
<property name="game">
<set>
<value>wangzhe</value>
<value>lol</value>
<value>galname</value>
</set>
</property>
<!--空指针null注入 -->
<property name="wife">
<null></null>
</property>
<!--properties常量注入 -->
<property name="infor">
<props>
<prop key="id">20200802</prop>
<prop key="name">cbh</prop>
</props>
</property>
</bean>
</beans>
使用p和c命名空间需要导入xml约束
xmlns:p=“http://www.springframework.org/schema/p”
xmlns:c=“http://www.springframework.org/schema/c”
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空间注入/set注入,可以直接注入属性的值-》property-->
<bean id="user" class="com.li.pojo.User" p:name="020" p:id="20" >
</bean>
<!--c命名空间,通过构造器注入,需要写入有参和无参构造方法-》construct-args-->
<bean id="user2" class="com.li.pojo.User" c:name="123" c:id="22"></bean>
</beans>
8.Bean的自动装配
- 自动装配是Spring满足bean依赖的一种方式
- Spring会在上下文自动寻找,并自动给bean装配属性
-
在xml中显示配置
-
在java中显示配置
-
隐式的自动装配bean
9.AOP
提供声明式事务,允许用户自定义切面
- 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等…
- 切面(Aspect):横切关注点 被模块化的特殊对象。即,它是一个类。(Log类)
- 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。(Log类中的方法)
- 目标(Target):被通知对象。(生成的代理类)
- 代理(Proxy):向目标对象应用通知之后创建的对象。(生成的代理类)
- 切入点(PointCut):切面通知执行的”地点”的定义。(最后两点:在哪个地方执行,比如:method.invoke())
- 连接点(JointPoint):与切入点匹配的执行点。
切入点可以理解为被增强的方法 要扩展功能的那个方法