Spring入门【一】
一、spring的概述
1. spring 的 开源的轻量级框架
2. spring的两大核心:IOC ,AOP
Spring框架是一个开源的Java平台,它为企业级应用开发提供了全面的解决方案。自从2003年首次发布以来,Spring框架已经成为了Java开发者的首选之一。Spring框架的核心目标是简化企业级应用的开发和维护,通过提供一致、灵活且易于扩展的编程模型,帮助开发者更快地构建高质量的应用程序。
在过去的几年里,Spring框架不断发展壮大,引入了许多新的功能和技术。例如,Spring Boot项目使得开发者能够快速搭建和运行基于Spring的应用程序,无需繁琐的配置和依赖管理。此外,Spring Cloud项目为微服务架构提供了一套完整的解决方案,包括服务发现、配置中心、断路器等功能。
Spring框架的核心组件包括:
- Spring Core:提供了一组基础功能,如依赖注入(DI)、切面编程(AOP)和事务管理等。
- Spring AOP:提供了面向切面编程的支持,允许开发者将横切关注点(如日志记录、安全性检查等)与业务逻辑分离。
- Spring DAO:提供了对JDBC和ORM框架的支持,简化了数据访问层的开发。
- Spring MVC:提供了一种基于请求-响应模式的Web应用程序框架,支持RESTful风格的API设计。
- Spring WebFlux:基于Reactor和Project Reactor构建的异步Web框架,支持非阻塞式IO操作和响应式编程模型。
- Spring Data:提供了一组用于访问各种数据存储技术的抽象层,如JPA、MongoDB、Redis等。
- Spring Security:提供了一套安全框架,用于保护Spring应用程序免受未经授权访问和其他安全威胁。
总之,Spring框架以其强大的功能、灵活的设计和广泛的社区支持,成为了Java开发者在企业级应用开发中不可或缺的一部分。无论是构建简单的Web应用程序还是复杂的企业级系统,Spring框架都能帮助开发者轻松实现高效、可扩展和可靠的应用程序。
二、spring的EJB的区别
1. EJB可以说像是一个Web Service,但也不完全是,比如EJB将编写好的业务组件放置在EJB容器上,然后提供接口给客户端访问;但是功能不仅限如此,EJB标准中提供了很多规范等,而这些规范只有在EJB容器才能正常运行。
EJB重量级框架
2. Spring容器取代了原有的EJB容器,因此以Spring框架为核心的应用无须EJB容器支持,可以在Web容器中运行。
Spring容器管理的不再是复杂的EJB组件,而是POJO(Plain Old Java Object) Bean。
Spring轻量级框架
三、耦合和解耦
1. 什么是耦合
模块之间的关联程度, 依赖程度
2. 什么是解耦
降低模块之间的耦合度(依赖关系)
3. 解耦的目的
编译器不依赖,运行期才依赖
4. 解耦思路
1) 把全限类名都放到配置文件中
2) 通过工厂帮助创建对象
四、解耦代码–自定义IOC
try {
Driver driver = new Driver();
//注册驱动
DriverManager.registerDriver(driver);
} catch (SQLException e) {
e.printStackTrace();
}
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
五、控制反转-- 自定义IOC
1. 引入依赖
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
2. 配置文件
<beans>
<!--
用bean标签配置所有的bean对象
id: 对象的唯一的标志
class:类名
-->
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl"></bean>
</beans>
3. 创建BeanFactory工厂对象
package com.itheima.factory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 创建bean对象的工厂类
*/
public class BeanFactory {
private static Map<String ,Object> map = new HashMap<>();
//提前把所有的对象创建出来,存储
//Map :因为有查找需求 --相当于容器对象-- 包含了所有的对象
//静态代码块
static{
//获取配置文件的输入流对象
InputStream inputStream = BeanFactory.class.getResourceAsStream("/beans.xml");
//解析xml获取xml中所有的信息
SAXReader reader = new SAXReader();
try {
//创建文档对象
Document doc = reader.read(inputStream);
//获取根节点
Element root = doc.getRootElement();
//获取根节点中所有的子节点
//element("bean") : 获取第一个叫bean的子节点
//elements("bean") : 获取所有叫bean的子节点
//elements() : 获取所有的子节点
List<Element> beanList = root.elements("bean");
//获取每一个bean的id和class
for (Element element : beanList) {
String id = element.attributeValue("id");
String className = element.attributeValue("class");
//通过className全限类名创建对象--获取字节码
Class clazz = Class.forName(className);
//通过反射创建对象
Object obj = clazz.newInstance();
//存储在Map集合中
//key: id
//value:obj
map.put(id ,obj);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//需要实现的功能,传入一个名字,获取一个Bean对象
public static Object getBean(String name){
return map.get(name);
}
}
4. 测试
Object userService1 = BeanFactory.getBean("userService");
System.out.println(userService1);
Object userService2 = BeanFactory.getBean("userService");
System.out.println(userService2);
六、spring的IOC入门(掌握)
1. 引入依赖
<!--引入spring 的最核心依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
2. 创建beans.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!--
http://www.springframework.org/schema/beans: 引入bean的名称空间
约束:现在xml的书写
dtd:mybatis
schema:spring
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd 引入约束
-->
<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="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
</beans>
3. 创建容器对象,根据id获取对象
//创建spring的IOC容器
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
//获取对象
Object userDao = ac.getBean("userDao");
System.out.println(userDao);
六、IOC的细节
1、容器对象的类结构图
a、beanFactory 是spring容器的顶层接口
b、接口ApplicationContext是 beanFactory子接口
实现类:ClassPathXmlApplicationContext -- 从类路径之下读取配置文件 (常用)
FileSystemXmlApplicationContext - 从绝对路径指定的配置文件读取
AnnotationConfigApplicationContext -- 纯注解配置使用的类 (常用)
c、BeanFactory与ApplicationContext区别
Resource resource = new ClassPathResource("beans.xml");
// BeanFactory:创建容器对象时,只是加载了配置文件,没有创建对象
// 获取对象时:创建对象
BeanFactory beanFactory = new XmlBeanFactory(resource);
//
Object userDao = beanFactory.getBean("userDao");
System.out.println(userDao);
//创建spring的IOC容器
//在创建容器对象时,创建对象 , (常用)
//ApplicationContext:在创建容器时只创建单例模式的对象
// 多例模式的对象,在获取时创建
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
Object userDao2 = ac.getBean("userDao");
System.out.println(userDao2);
2、getBean方法
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//根据名称获取该对象
// Object userDao = ac.getBean("userDao");
// System.out.println(userDao);
// 根据类型获取该对象
// 如果该类型有两个实现类,会执行异常
// UserDao userDao = ac.getBean(UserDao.class);
// System.out.println(userDao);
// 得到是id为userDao1的对象,类型为UserDao接口的实现类
UserDao userDao = ac.getBean("userDao2", UserDao.class);
System.out.println(userDao);
3、bean对象的范围和生命周期
springIOC容器默认的单例模式: 单例模式的对象在创建容器时创建,销毁容器时销毁
scope:prototype 原型模式(多例模式):获取时创建,当对象长时间不在被引用, 则被垃圾回收机制回收
scope:singleton :单例模式
scope :request: 请求范围
scope: session : 回话范围
scope:global session :全局范围 -- spring 5的新特性中被删除了
UserDao userDao1 = ac.getBean("userDao", UserDao.class);
System.out.println(userDao1);
UserDao userDao2 = ac.getBean("userDao", UserDao.class);
System.out.println(userDao2);
4、实例化bean的三种方法
1 .<bean id="name" class="全限类名">
getBean(name)
2. 根据静态工厂获取对象
/**
* 通过静态工厂获取对象
*/
public class StaticFactory {
public static UserDao getUserDao(){
return new UserDaoImpl();
}
}
<!--通过静态工厂创建UserDao对象-->
<!--
factory-method: 工厂方法,返回UserDao对象的方法名
-->
<bean id="userDao" class="com.itheima.factory.StaticFactory" factory-method="getUserDao"></bean>
3. 根据实例(非静态)工厂获取对象
/**
* 实例工厂创建对象
*/
public class InstanceFactory {
public UserDao getUserDao(){
return new UserDaoImpl();
}
}
<!--创建实例工厂对象-->
<bean id="instanceFatory" class="com.itheima.factory.InstanceFactory"></bean>
<!--通过实例工厂创建UserDao对象-->
<bean id="userDao2" factory-bean="instanceFatory" factory-method="getUserDao"></bean>
5、依赖注入
a.什么是依赖注入
业务层需要持久层的对象,在配置文件中给业务层传入持久层的对象,就是依赖注入
b. ioc
控制反转包含了依赖注入和依赖查找
6、构造方法注入
<!--依赖注入-->
value 属性只能赋值简单的类型:基本数据类型和String类型
ref: pojo类型,复杂类型, 关联创建好的对象
<!--默认创建对象方式,使用默认的空的构造方法创建对象-->
<bean id="user" class="com.itheima.domain.User">
<!--通过构造方法参数的索引赋值-->
<!--<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>
7、set方法注入属性(常用)
<!--通过set方法注入-->
<bean id="user2" class="com.itheima.domain.User">
<!--property :属性注入 , 先找到set方法,才能最终找到属性-->
<property name="username" value="王朝"></property>
<property name="birthday" ref="birthday"></property>
<property name="sex" value="男"></property>
<property name="id" value="4"></property>
</bean>
8、p名称空间注入:基于set方法注入
a、在头部文件中引入p名称空间
b、使用p名称空间注入属性
<bean id="user3" class="com.itheima.domain.User"
p:id="5" p:username="马汉" p:sex="男" p:birthday-ref="birthday"
></bean>
9、注入集合属性
<!--注入集合属性-->
<bean id="user4" class="com.itheima.domain.User">
<!--array ,list ,set 结构相同,标签可以混用-->
<property name="list">
<set>
<value>javaSE</value>
<value>javaEE</value>
<value>javaME</value>
</set>
</property>
<property name="set">
<array>
<value>javaSE</value>
<value>javaEE</value>
<value>javaME</value>
</array>
</property>
<property name="strs">
<list>
<value>javaSE</value>
<value>javaEE</value>
<value>javaME</value>
</list>
</property>
<!--map集合properties结构相同,可以通用-->
<property name="map">
<props>
<prop key="五" >five</prop>
<prop key="六" >six</prop>
<prop key="七" >seven</prop>
</props>
</property>
<property name="properties">
<map>
<!--键值对配置-->
<entry key="一" value="one"></entry>
<entry key="二" value="two"></entry>
<entry key="三" value="three"></entry>
<entry key="四" value="four"></entry>
</map>
</property>
</bean>
Spring入门【二】
一、使用IOC完成改造CRUD
a、引入依赖
<!--spring的核心包(基本)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--dbutils-->
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.4</version>
</dependency>
<!--c3p0数据源-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
b、创建表
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);
c、实体类
public class Account {
private Integer id;
private String name;
private Float money;
}
d、持久层
1 接口
package com.torlesse.dao;
import com.torlesse.domain.Account;
import java.util.List;
public interface AccountDao {
/**
* 查询全部
* @return
*/
public List<Account> findAll();
/**
* 根据id查询
* @param id
* @return
*/
public Account findById(Integer id);
/**
* 保存账户
* @param account
*/
public void save(Account account);
/**
* 更新账户
* @param account
*/
public void update(Account account);
/**
* 根据id删除账户
* @param id
*/
public void del(Integer id);
}
2. 实现类
package com.torlesse.dao.impl;
import com.torlesse.dao.AccountDao;
import com.torlesse.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
/**
* dbUtils 操作数据库
* 入口:QueryRunner对象
*/
public class AccountDaoImpl implements AccountDao {
QueryRunner queryRunner;
public void setQueryRunner(QueryRunner queryRunner) {
this.queryRunner = queryRunner;
}
@Override
public List<Account> findAll() {
String sql = "select * from account";
try {
List<Account> accountList = queryRunner.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 {
Account account = queryRunner.query(sql, new BeanHandler<>(Account.class), id);
return account;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public void save(Account account) {
String sql = "insert into account values(null , ? , ?)";
try {
queryRunner.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 {
queryRunner.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 {
queryRunner.update(sql ,id);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
e、业务层
1. 接口
package com.torlesse.service;
import com.torlesse.domain.Account;
import java.util.List;
public interface AccountService {
/**
* 查询全部
* @return
*/
public List<Account> findAll();
/**
* 根据id查询
* @param id
* @return
*/
public Account findById(Integer id);
/**
* 保存账户
* @param account
*/
public void save(Account account);
/**
* 更新账户
* @param account
*/
public void update(Account account);
/**
* 根据id删除账户
* @param id
*/
public void del(Integer id);
}
2. 实现类
package com.torlesse.service.impl;
import com.torlesse.dao.AccountDao;
import com.torlesse.domain.Account;
import com.torlesse.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);
}
}
f、配置文件
<?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">
<!--在测试时需要用到Service对象,创建service对象-->
<bean id="accountService" class="com.torlesse.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao" ></property>
</bean>
<!--在AccountService中需要AccountDao对象-->
<bean id="accountDao" class="com.torlesse.dao.impl.AccountDaoImpl">
<property name="queryRunner" ref="queryRunner"></property>
</bean>
<!--在AccountDao中需要QueryRunner对象,创建对象-->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<!--按照类型在构造方法注入数据源对象-->
<constructor-arg type="javax.sql.DataSource" ref="dataSource"></constructor-arg>
</bean>
<!--在QueryRunner对象中需要DataSource对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!--通过set方法注入必要的四个属性-->
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_330"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
g、测试类
package com.torlesse;
import com.torlesse.domain.Account;
import com.torlesse.service.AccountService;
import com.torlesse.service.impl.AccountServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class TestCRUD {
@Test
public void testFindAll(){
//创建容器
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);
}
}
@Test
public void testFindById(){
//创建service对象
AccountService accountService = new AccountServiceImpl();
Account account = accountService.findById(1);
System.out.println(account);
}
@Test
public void testSave(){
//创建service对象
AccountService accountService = new AccountServiceImpl();
Account account = new Account();
account.setName("ddd");
account.setMoney(1111F);
accountService.save(account);
}
@Test
public void testUpdate(){
//创建service对象
AccountService accountService = new AccountServiceImpl();
Account account = new Account();
account.setId(4);
account.setName("eeee");
account.setMoney(1222F);
accountService.update(account);
}
@Test
public void testDel(){
AccountService accountService = new AccountServiceImpl();
accountService.del(4);
}
}
二、常用的注解
a. 注解开发入门流程
1.引入依赖
<!--引入spring的核心包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--引入单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
2. 配置文件:applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!--
开启注解,指定扫描的包 : context:component-scan
引入context名称空间-引入约束
base-package:指定要扫描的包, 扫描的是包及其子包
-->
<context:component-scan base-package="com.torlesse"></context:component-scan>
<!--
context:include-filter :指定包含过滤
type="annotation": 按照类型过滤
expression: 过滤的表达式
只扫描标记了Controller注解的类
-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter>
<!--
context:exclude-filter:指定排除过滤
type="annotation": 按照类型过滤
expression:过滤的表达式
排除标记了Controller的注解都会扫描
-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</beans>
3. 在需要创建对象的类上添加注解
@Component
4. 测试
//创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取对象
UserDao userDao = ac.getBean(UserDao.class);
System.out.println(userDao);
UserService userService = ac.getBean(UserService.class);
System.out.println(userService);
b. @Component -- 标记在类上,不能用在方法上
作用:创建对象, 只要标记了,扫描了该包,对象就会创建
衍生了三个子注解
@Controller 一般用于web(控制层)层
@Service 一般用于业务层
@Repository 一般用于持久层
相当于xml
<bean id="" class="全限类名"></bean>
属性:value="userDao" 相当于xml的 id="userDao"
如果没有指定value属性,默认的名称是 简单类名,首字母小写
UserDaoImpl -- userDaoImpl
UserServiceImpl -- userServiceImpl
c. @Autowired -- 自动注入
可以标记在属性和set方法上,如果标记在属性上,可以没有set方法
特点:默认自动按照类型注入
流程:当属性|set方法标记了@Autowired ,会自动在容器中查询该属性类型的对象,如果有且只有一个,则注入
@Qualifier :必须与@Autowired结合使用
作用:如果自动注入按照类型注入失败,则按照指定的名称注入
如果没有@Qualifier,类型注入失败,则按照属性名按照名称注入
d. @Resource -- 自动注入
流程:当属性|set方法标记了@Resource,会自动按照名称注入, 如果名称没有找到,则根据类型注入,如果类型有多个,则抛出异常
e. @Autowired :默认按照类型注入,如果类型有多个,则按照名称注入 -- spring提供的
@Resource : 默认按照名称注入,没有名称没有找到,按照类型注入 -- jdk提供的
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
f. @Configuration : 标记该类为配置文件类
可以替换 applicationContext.xml
g. @ComponentSacn("com.torlesse")
相当于:<context:component-scan base-package="com.torlesse">
h. @Import: 引入其他配置文件
<import resource="classpath:applicationContext-dao.xml"></import>
i. @Bean -- 通过方法创建对象,一般用于创建别人提供的类
相当于:<bean >
j. @Scope("singleton|prototype")
配置对象的范围:相当于:bean标签中的属性 scope="singleton|prototype"
k. 生命周期
@PostConstruct:相当于bean标签的属性 init-method,指定初始化方法
@PreDestroy:相当于bean标签的属性:destroy-method, 指定对象的销毁方法
l. @Value 给属性赋值 -- 只能赋值简单类型
m. PropertySource : 引入外部属性文件
相当于:<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
三、使用注解改造账户CRUD
a、实体类
public class Account {
private Integer id;
private String name;
private Float money;
}
b、持久层
package com.torlesse.dao.impl;
import com.torlesse.dao.AccountDao;
import com.torlesse.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.sql.SQLException;
import java.util.List;
/**
* dbUtils 操作数据库
* 入口:QueryRunner对象
*/
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
@Autowired
QueryRunner queryRunner;
@Override
public List<Account> findAll() {
String sql = "select * from account";
try {
List<Account> accountList = queryRunner.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 {
Account account = queryRunner.query(sql, new BeanHandler<>(Account.class), id);
return account;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public void save(Account account) {
String sql = "insert into account values(null , ? , ?)";
try {
queryRunner.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 {
queryRunner.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 {
queryRunner.update(sql ,id);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
c、业务层
package com.torlesse.service.impl;
import com.torlesse.dao.AccountDao;
import com.torlesse.domain.Account;
import com.torlesse.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private 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);
}
}
d、配置文件
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解,扫描包-->
<context:component-scan base-package="com.torlesse"></context:component-scan>
<!--创建queryRunner对象, 需要数据源对象,创建数据源对象,通过构造方法注入-->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<!--通过构造方法参数类型注入-->
<constructor-arg type="javax.sql.DataSource" ref="dataSource"></constructor-arg>
</bean>
<!--创建数据源对象:需要注入四个参数-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--通过set方法注入-->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_331"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
e、测试
package com.torlesse;
import com.torlesse.domain.Account;
import com.torlesse.service.AccountService;
import com.torlesse.service.impl.AccountServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
/**
* 1. 替换Junit的运行器: 为spring与junit整合后的运行器
* 2. 指定配置文件路径, 会自动创建容器对象
* @ContextConfiguration({"classpath:applicationContext.xml"})
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext.xml"})
public class TestCRUD {
// ApplicationContext ac;
// @Before
// public void init(){
// //创建容器
// ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//
// }
@Autowired
AccountService accountService;
@Test
public void testFindAll(){
//创建service对象
List<Account> accountList = accountService.findAll();
for (Account account : accountList) {
System.out.println(account);
}
}
@Test
public void testFindById(){
//创建service对象
Account account = accountService.findById(1);
System.out.println(account);
}
@Test
public void testSave(){
//创建service对象
AccountService accountService = new AccountServiceImpl();
Account account = new Account();
account.setName("ddd");
account.setMoney(1111F);
accountService.save(account);
}
@Test
public void testUpdate(){
//创建service对象
AccountService accountService = new AccountServiceImpl();
Account account = new Account();
account.setId(4);
account.setName("eeee");
account.setMoney(1222F);
accountService.update(account);
}
@Test
public void testDel(){
AccountService accountService = new AccountServiceImpl();
accountService.del(4);
}
}
四、纯注解开发
1. SpringConfiguration.java
/**
* 1. 标记该类为配置文件类 @Configuration
* 2. 指定注解扫描的包路径 @ComponentScan({"com.torlesse"})
* 3. 引入其他配置文件类 @Import({JDBCConfiguration.class})
*/
@Configuration
@ComponentScan({"com.torlesse"})
@Import({JDBCConfiguration.class})
public class SpringConfiguration {
}
2. JDBCConfiguration.java
/**
* 配置持久层的对象
* @Configuration :可以省略的
*
* @Bean("name") 用在方法上,用来指定方法创建的对象存到容器中
* "name": 就是在容器的名称
*/
@Configuration
public class JDBCConfiguration {
@Bean("queryRunner")
public QueryRunner createQueryRunner(DataSource dataSource){
// 需用通过构造方法注入dataSource
QueryRunner queryRunner = new QueryRunner(dataSource);
return queryRunner;
}
/**
* 创建数据源对象:dataSource
* @return
*/
@Bean("dataSource")
public DataSource createDataSource(){
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/spring_331");
dataSource.setUser("root");
dataSource.setPassword("root");
try {
dataSource.setDriverClass("com.mysql.jdbc.Driver");
} catch (PropertyVetoException e) {
e.printStackTrace();
}
return dataSource;
}
}
3. 测试
//纯注解创建容器对象
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
//创建service对象
AccountService accountService = ac.getBean("accountService",AccountService.class);
六、spring与junit的整合
1. 引入依赖
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--引入spring-5 的测试包: 必须引用相应的junit包, junit的版本必须是4.12以上-->
<!--引入spring-4 的测试包: 必须引用相应的junit包, junit的版本必须是4.9以上-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
2.1 配置测试环境-- xml
a. 替换Junit的运行器: 为spring与junit整合后的运行器
@RunWith(SpringJUnit4ClassRunner.class)
b. 指定配置文件路径, 会自动创建容器对象, 必须添加classpath
@ContextConfiguration({"classpath:applicationContext.xml"})
2.2 配置测试环境-- ann
a. 替换Junit的运行器: 为spring与junit整合后的运行器
@RunWith(SpringJUnit4ClassRunner.class)
b. 指定配置文件路径, 会自动创建容器对象, 必须添加classpath
@ContextConfiguration(classes = {SpringConfiguration.class})
3. 测试:从容器可以获取某类型的对象
@Autowired
AccountService accountService;
书籍推荐
以下是一些关于Spring框架的书籍推荐:
- 《Spring揭秘》:这本书是一本经典的Spring入门书籍,适合想要深入了解Spring框架的开发者。
- 《精通Spring 4.x》:这本书以实战为主,通过实例讲解了Spring框架的各种组件,适合想要快速掌握Spring框架的开发者。
- 《Spring Boot实战》:这本书介绍了如何使用Spring Boot快速构建Web应用程序,适合想要学习如何使用Spring Boot开发的开发者。
- 《Spring核心技术》:这本书系统地介绍了Spring框架各个基本组件的基本使用,适合想要全面了解Spring框架的开发者。
希望对你有帮助~