Spring概念:
1.Spring时开源的轻量级框架
(1).aop:面向切面编程,扩展功能不是修改源代码实现
(2).ioc:控制反转,对象的创建不是通过new方式实现的,而是交给配置创建对象
需要导入一个commons-logging-1.1包
//加载Spring配置文件,根据配置文件创建对象
ApplicationContext context = new ClassPathXmlApplicationContext(“spring-config.xml”);
//得到配置创建对象
User user = (User)context.getBean(“user”);
bean标签常用属性
id属性:起名称,根据id值得到配置对象
class属性:创建对象所在类的全路径
name属性:功能和id是一样的,name中可以包含特殊符号
scope属性:
-singleton:默认值,单例
-prototype:多例
-request:创建对象把对象放到request域里面去
-session:创建对象把对象放到session域里面
-globalSession:创建对象把对象放到globalSession域里面
设置有参构造,能够利用带参构造方法,设置属性值
<bean id="demo" class="cn.itcast.ioc.PropertyDemo">
<constructor-arg name="name" value="jack"></constructor-arg>
</bean>
使用set方法注入属性
<bean id="book" class="cn.itcast.ioc.Book">
<!--注入属性-->
<property name="bookName" value="Chinese"></property>
</bean>
使用接口注入属性:
<bean id="userdao" class="cn.itcast.ioc.UserDao"></bean>
<bean id="userService" class="cn.itcast.ioc.UserService">
<!--注入属性-->
<property name="userDao" ref="userDao"></property>
</bean>
使用名称空间注入
xmlns:p="http://www.springframework.org/schema/p"
<bean id="person" class="cn.itcast.ioc.Person" p:name="jack"></bean>
注入复杂类型:
1.数组
2.list集合
3.map集合
4.properties类型
IOC和DI的区别:
(1)IOC:控制反转,对象的创建不是通过new方式实现的,而是交给配置创建对象
(2)DI:依赖注入,向类里面的属性设置值
(2)关系:依赖注入不能单独存在,需要在IOC基础之上完成
常见对象有四个注解
@component
@Controller : web层
@Service :业务层
@Repository :持久层
<!--开启注解的扫描-->
<context:component-scan base-package="cn.itcast.anno"></context:component-scan>
@Service (value = “userService”)
public class UserService {
Component(value = “userDao”)
public class UserDao {
//在dao属性使用注解,完成对象注入
@Autowired
private UserDao userDao;
name属性值写注解创建dao对象,value值
@Resource(name=“userDao”)
private UserDao userDao;
AOP概念
1.aop: 面向切面(方面)编程,扩展功能不修改源代码
2.AOP采取横向抽取机制,取代了传统纵向继承重复性代码
AOP原理:
1.
连接点:类里面的那些方法可以被增强,这些方法称为连接点
切入点:在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面的add方法和update方法
实际增强的方法称为切入点
通知/增强: 增强的逻辑,称为增强,比如扩展日志功能,这个日志功能称为增强
通知分为
前置通知,在方法之前执行
后置通知,在方法之后执行
异常通知,方法中出现异常
最总通知,在后置之后执行
环绕通知:在方法之前和之后执行
切面:把增强应用到具体方法上面,过程称为切面,把增强用到切入点过程
spring的aop操作
1.在spring里面进行aop操作,使用aspectj实现
aspectjweaver
spring-aspects
aopalliance
aspectj不是spring一部分,和spring一起使用来进行aop操作
2.aspectj实现aop有两种法师
(1).基于aspectj的xml配置
(2).基于aspectj的注解方式
使用表达式配置切入点:
1.切入点,实际增强的方法
2.常用的表达式
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
(1)execution(* cn.itcast.aop.Book.add(…))
(2). execution(* cn.itcast.aop.Book.(…))
(3). execution( .(…))
(4). 匹配所有save开头的方法 execution(* save*(…))
<bean id="book" class="cn.itcast.aop.Book"></bean>
<bean id="myBook" class="cn.itcast.aop.MyBook"></bean>
<!--配置aop操作-->
<aop:config>
<!--配置切入点-->
<aop:pointcut id="pointcut1" expression="execution(* cn.itcast.aop.Book.*(..))"></aop:pointcut>
<!--配置切面-->
<aop:aspect ref="myBook">
<!--
配置增强类型
method:增强类里面使用那个方法作为前置
-->
<aop:before method="before" pointcut-ref="pointcut1"></aop:before>
</aop:aspect>
</aop:config>
log4j介绍
1.可以通过log4j看到程序运行过程中更详细的信息
(1) 经常使用log4j查看日志
2.使用
基于aspectj的注解aop操作:
@Before(value = “execution(* cn.itcast.dao.Book.*(…))”)
jdbcTemplate对jdbc进行封装
spring-jdbc
spring-tx
查询的具体实现:
查询返回某一个值
template.queryForObject(String sql,Class requiredType);
(1).第一个参数是sql语句
(2).第二个参数是返回什么对象
查询返回对象
template.queryForObject(String sql,RowMapper rowMapper,Object… args);
第一个参数是sql语句
第二个参数是RowMapper,是借口,类似于dbutils里面接口
第三个参数是可变参数
需要手动实现接口
class MyRowMapper implements RowMapper{}
查询返回list集合
template.query(String sql,RowMapper rowMapper);
1.spring配置c3p0连接池
c3p0.jar
mchange-commons-java.jar
(1).
<bean id="dataSources" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--注入属性值-->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring_day03"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
(2).
<bean id="userDao" class="cn.itcast.c3p0.UserDao">
<!--注入JdbcTemplate对象-->
<property name="template" ref="template"></property>
</bean>
<!--创建JdbcTemplate对象-->
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSources"></property>
</bean>
<bean id="userService" class="cn.itcast.c3p0.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
spring事务管理:
声明式事务管理:(1).基于xml配置文件实现
(2).基于注解实现
接口:platformTransactionManager
spring配置文件xml全约束
<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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
</beans>
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
数据库无法匹配中文
str=new String(str.getBytes(“ISO8859-1”),“UTF-8”));
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入dataSources-->
<property name="dataSource" ref="dataSources"></property>
</bean>
<!--配置事务增强-->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<!--做事务操作-->
<tx:attributes>
<!--设置事务匹配规则-->
<tx:method name="account*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!--配置切入点-->
<aop:pointcut id="pointcut1" expression="execution(* cn.itcast.tramsform.OrdersService.*(..))"></aop:pointcut>
<!--配置切面-->
<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"></aop:advisor>
</aop:config>
注解方式配置事务管理器:
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入dataSources-->
<property name="dataSource" ref="dataSources"></property>
</bean>
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
在要使用事务的方法无偶在类添加注解
@Transactional
MyBatis:
约束:
<?xml version="1.0" encoding="UTF-8" ?>mapper接口需要遵循的四个原则
1.接口 方法名 == user.xml中id名
2.返回值类型与mapper.xml中返回值类型要一致
3.方法啊的入参类型与mapper.xml中入参的类型要一致
4.命名空间的值为该接口的路径名
String resource = “mybatis-config.xml”;
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//创建sqlsession
SqlSession session = sqlSessionFactory.openSession();
通过接口类制动生成一个实现类
UserMapper mapper = session.getMapper(UserMapper.class);
配置内容
SqlMapConfig.xml中配置内容和顺序如下
properties 属性
settings 全局配置参数
typeAliases 类型别名
typeHandlers 类型处理器
objectFactory 对象工厂
plugins 插件
environments 环境集合属性对象
environment环境子属性对象
transactionManager 事务管理
dataSource 数据源
mappers 映射器
idea中需要接入这段代码才可以使用package标签
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<resultMap id="orders" type="Orders">
<result property="userid" column="user_id"></result>
</resultMap>
可以直接用类的名字
<typeAliases>
<package name="cn.itcast.pojo"></package>
</typeAliases>
where标签可以去掉第一个前and
多个id查询
<select id="selectUserByIds" parameterType="QueryVo" resultType="User">
<include refid="selector"/>
<where>
<foreach collection="ids" item="id" separator="," open="id in (" close =")">
#{id}
</foreach>
</where>
</select>
<!-- //一对一关联查询、
public List<Orders> selectOrders();-->
<resultMap type="Orders" id="order">
<result column="id" property="id"/>
<result column="user_id" property="user_id"/>
<result column="number" property="number"/>
<!-- 一对一 -->
<association property="user" javaType="User">
<result column="user_id" property="id"/>
<result column="username" property="username"/>
</association>
</resultMap>
<select id="selectOrders" resultMap="order">
SELECT
o.id,
o.user_id,
o.number,
o.createtime,
u.id,
u.username
FROM orders o
left join user u
on o.user_id = u.id
</select>
isElIgonred=“false” 在jsp页面使用el表达式
springMVC
RequestMapping(value="", method=RequestMethod.post)
ModelAndView 无敌的,带着数据,返回视图路径
String 返回视图路径 return “redirect:url” 最好用重定向,别用转发
void ajax 请求 合适
@ResponseBody
@RequestBody //将字符串转换成json对象
springmvc拦截器
<mvc:interceptors>
<mvc:interceptor>
</mvc:interceptor>
</mvc:interceptors>