Spring5的总结
总结
一、Spring是什么?
功能 | 实现 |
---|---|
ioc | 注解法 |
aop | 增强方法 |
JdbcTemplate | 数据库操作 |
事务管理 | 事务的四大特性 |
二、使用步骤
1.ioc容器注解法配置
@Component(作用在类上);
@Repository:用于对dao层实现类进行标注(持久层);
@Service:用于对service层实现类进行标注(业务层);
@Controller:用于对Controller实现类进行标注(web层);
@Component
public class administratorsBean {
int id;
String login;
String phone;
String email;
String role;
String date;
String password;
代码如下(示例):
2.ioc容器用命名空间开启注解扫描
代码如下(示例):
<?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.example"></context:component-scan>
3.获取ioc容器对象
代码如下(
ApplicationContext context = new ClassPathXmlApplicationContext("xml配置文件");
获取的对象 = context.getBean("注解法value,默认为第一个字母小写", 对象.class);
例如上面容器对象为administratorsBean 则获取对象方法为
administratorsBean administrators = context.getBean("administratorsBean ", administratorsBean.class);
4.AOP增强方法实现
AOP为切面编程,分为3个点
1.连接点
2.切入点
3.增强点
增强点分为{
前置通知
后置通知
环绕通知
异常通知
最终通知
}
开启注解扫描:
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启注解扫描 -->
<context:component-scan basepackage="com.example"></context:component-scan>
5.AOP增强
在增强类上面添加注解 @Aspect和@Pointcut
@Pointcut的value为要增强的方法路径名称
代码如下(
@Component @Aspect
public class UserDaopro {
@Pointcut(value = "execution(* test3.UserDao.add(..))")
public void cut()
{}
@Before("cut()")
public void before()
{
System.out.println("before");
}
@After("cut()")
public void after()
{
System.out.println("after");
}
}
上面为简化的写法,用一个cut类来保存value,每一个路径只用调用cut方法就可以得到,下面是另一种易理解的写法
@Component @Aspect
public class UserDaopro {
@Before(value = "execution(* test3.UserDao.add(..))")
public void before()
{
System.out.println("before");
}
@After(value = "execution(* test3.UserDao.add(..))")
public void after()
{
System.out.println("after");
}
}
6.JdbcTemplate使用
@Component
public class goodsImpl implements goods{
@Autowired
private JdbcTemplate jdbcTemplate;
在goodsImpl方法中就可以用JdbcTemplate 的方法
1.增加.删除.更新 返回值为影响的数据行数
args为sql语句占位符数据
@Override
public int add(String sql) {
int update =0;
Object[] args = {};
try {
update = jdbcTemplate.update(sql,args);
}
catch (Exception exception)
{
}finally {
return update;
}
}
2.查询返回一个数
jdbcTemplate.queryForObject方法,但是此方法只能返回一个数,如果返回不是为一个数就报错,报错为预期返回1个数,真实返回X个数
@Override
public int select(String sql) {
int back = 0;
try {
back = jdbcTemplate.queryForObject(sql,Integer.class);//Interger为返回数的类型,如果想要float就变为float.class
}catch (Exception e)
{
}
return back;
}
3.查询返回一个对象的list集
需要用到BeanPropertyRowMapper,该方法为把数据转化为想要的对象ordersBean。
@Override
public List<ordersBean> selectList(String sql) {
List<ordersBean> ordersList = null;
try {
ordersList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<ordersBean>(ordersBean.class));
}catch (Exception e)
{}
finally {
return ordersList;
}
}
4.查询返回一个list集
该方法为返回一个正常的list集,键值对储存的list
@Override
public List<Map<String, Object>> selectForWelcome(String sql) {
List<Map<String, Object>> floats = null;
try {
floats = jdbcTemplate.queryForList(sql);}
catch (Exception exception)
{
exception.printStackTrace();
}finally {
return floats;
}
}
5.批量操作,在操作里面加一个for循环(建议学习MyBatis)
@Override
public int update(String sql, int[] ids, int[] nums) {
int update =0;
try {
for (int i = 0 ;i<ids.length;i++)
{
Object[] args = {nums[i],nums[i],nums[i],ids[i]};
jdbcTemplate.update(sql,args);
}
}
catch (Exception exception)
{
}finally {
return update;
}
}