- 浏览: 50824 次
文章分类
- 全部博客 (98)
- openstack (14)
- spring+hibernate+jdbc (15)
- dashboard (1)
- Python (3)
- nova (2)
- ubuntu (1)
- Linux (2)
- apache (1)
- openstack,错误 (1)
- Windows (1)
- horizon (1)
- websocket (1)
- java (2)
- mybatis (4)
- quartz (2)
- git (1)
- spring,错误 (1)
- angularJS (8)
- redis (2)
- vim (1)
- virsh (1)
- jsp (1)
- react (1)
- openstack,openstack安装 (4)
- 社区 (1)
最新评论
-
haoningabc:
前后端都会啊,全栈女神啊
angularJS 弹出层 -
haoningabc:
好厉害,偶像
openstack安装--keystone
1.spring配置文件:
applicationContext.xml
该配置文件里需配置dataSource、transactionManager、sqlSessionFactory、MapperScannerConfigurer、所要用到的接口
2.web.xml配置文件
配置监听器,当web启动时,自动加载spring配置文件
配置DispatcherServlet,路由与controller的映射
3.servlet配置文件mvc-dispatcher-servlet.xml
配置基础扫描包(扫描controller),配置mvc:resources,配置视图解析器
4.写控制器
代码参见:http://blog.csdn.net/techbirds_bao/article/details/9233599/
OR M
O:对象,自己定义一个class,属性为private ,set与get方法
R:relational关系型数据库
M:mapping。*.hbm.cfg定义类与表的映射,变量与字段的映射,当没有column字段时,默认column=name
-----------------------
读取配置文件:(整个工程只初始化一次)
Configuration cfg=new Configuration();
cfg.configure("config.cfg.xml");
SessionFactory sessionFactory=cfg.buildSessionFactory();
--------------
模板代码
Session session=null;
Transaction tx=null;
try{
session=sessionFactory.openSession();
tx=session.beginTransaction();
..............代码
tx.commit();
}catch(Exception e){
if(tx!=null){
tx.rollback();
}
throw e;
}finally{
if(session!=null)session.close();
}
---------------------------------------------------------
在*.xml配置文件中设置bean
<bean id="helloWorld" class="spring.bean.HelloWorld">
<!-- 为属性赋值 ,通过set方法-->
<property name="name" value="zouhuiying"></property>
</bean>
<bean id="book" class="spring.bean.Constructor1">
<!--通过构造方法赋值-->
<constructor-arg value="math"></constructor-arg>
<constructor-arg value="180"></constructor-arg>
<!--通过index(构造方法中变量的索引)标识,或者type(变量的类型)-->
</bean>
![CDATA[]]特殊字符
<value></value>:
<bean id="book" class="spring.bean.Constructor1">
<constructor-arg>
<value>![CDATA[<math^>]]</value>
</constructor-arg>
</bean>
内部bean
<bean id="" class="">
<property name="">
<!--内部bean不能被外部使用,所以可以不加id属性-->
<bean class="">
<constructor-arg>
<value><![CDATA[<math>*]]></value>
</constructor-arg>
</bean>
</property>
</bean>
赋值为null:
<constructor-arg><null/></constructor-arg>
级联属性赋值(内部类的属性):属性需要先初始化后,才可以为级联属性赋值,否则处异常
-------------------------------------------------------
配置集合属性
list<car>
<property name="">
<list>
<ref bean="" />
</list>
</property>
------------
使配置map属性
<property name="">
<map>
<entry key="" value-ref="">
</entry>
</map>
</property>
-------------
<bean>
<property>
<props>
<prop key="">value</prop>
<prop key="">value</prop>
</props>
</property>
</bean>
-------------
配置单例的集合bean
<property name="car" ref="cars">
<util:list id="cars">
<ref bean="car1"/>
<ref bean="car2"/>
</util:list>
------------
p:命名空间
xmlns:p="http://www.springframework.org/schema/p"
<bean id="person2" class="spring.bean.Person" p:name="baobao" p:age="23" p:car-ref="car1" />
---------------
配置properties属性值
Properties类
private Properties properties;
<bean id ="datasource" class="spring.bean.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">zouhuiying</prop>
</props>
</property>
</bean>
-------------------------------------------------------
获取bean
首先获取IOC容器ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
即:加载配置文件,获取IOC容器后,系统会自动执行配置文件中bean的set方法和构造函数
HelloWorld hello=(HelloWorld) ctx.getBean(HelloWorld.class); //通过类名
Constructor1 con=(Constructor1) ctx.getBean("book2");//通过id
--------------------------------------------------------
自动装配
autowire=""
byName:set方法的名字bean的id //map bean不能自动装配
byType:class的类型。只能有一个bean
-----------------------------------------------------------
配置的继承parent parent="person"
abstract="True" 抽象bean不能被实例化
如果没有指定class,必须要设置abstract="True"
子类重写父类的property时,只能用父类已经用过的属性
bean直接的依赖
depends-on="某个bean的id
-----------------------------------------------------------
bean的作用域
使用<bean></bean>是默认是sscope=ingleton单例,每次调用都是同一个bean
设置scope属性prototype(原型)每次用bean时,都是产生一个新的bean,初始化IOC容器时,并不初始化该bean
-----------------------------------------------------------
使用外部属性文件
spring要连接数据库,需要配置dataSource bean,class="系统的datasource"
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
${var}
--------------------------------------------------------------
spring语言表达式:SpEL
#{...} //...代表SpEL,动态赋值
value="#{'gggggggg'}" //直接赋值
T():引用系统函数
#{T(java.lang.Math).PI * 80}
-----------------------------------------------------------------
bean生命周期
init-method="init" //自己写初始化和销毁方法
destroy-method="destroy"
ClassPathXmlApplicationContext对象有个close方法
ApplicationContext没有close放法
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beanss.xml");
Car car=(Car) ctx.getBean("car");
System.out.println(car);
ctx.close();
}
<bean id="car" class="spring.beans.Car" p:brand="Audi" p:color="red" init-method="init" destroy-method="destroy" />
bean的后置处理器:不需要配置id,IOC容器自动识别
MyBeanPostProcessor继承BeanPostProcessor接口,实现方法postProcessAfterInitialization与postProcessBeforeInitialization(Object bean, String beanName)
配置文件:<bean class="spring.beans.MyBeanPostProcessor" />
-----------------------------------------------------------------------------------
通过调用静态工厂方法创建 Bean
class Car
public class StaticFactory {
private static Map<String, Car> cars=new HashMap<String, Car>();
static{
cars.put("car1", new Car("house","red"));
cars.put("car2", new Car("audi","white"));
}
public static Car getCar(String name){
return cars.get(name); //调用的是car的工厂方法
}
}
配置文件:
<bean id="car1" class="springfactory.StaticFactory" factory-method="getCar">
<constructor-arg value="car1"></constructor-arg>
</bean>
main函数
ApplicationContext ctx=new ClassPathXmlApplicationContext("factory.xml");
Car car=(Car)ctx.getBean("car1");
System.out.println(car);
----------------------------
通过调用实例工厂方法创建 Bean
public class InstanceFactory {
private Map<String, Car> cars=null;
public InstanceFactory() {
super();
cars=new HashMap<String, Car>();
cars.put("one", new Car("ford","yellow"));
cars.put("two", new Car("anta","green"));
}
public Car getCar(String name) {
return cars.get(name);
}
配置文件:
<!-- 配置实例工厂 -->
<bean id="factory" class="springfactory.InstanceFactory"></bean>
<bean id="car2" factory-bean="factory" factory-method="getCar">
<constructor-arg value="two"></constructor-arg>
</bean>
------------------------------------------------------------------
通过FactoryBean配置bean
-------------------------------------------------------------------
配置扫描包,多个包用“,”分隔
<context:component-scan base-package="com.zou.service"></context:component-scan>
@Component: 基本注解, 标识了一个受 Spring 管理的组件
@Respository: 标识持久层组件
@Service: 标识服务层(业务层)组件
@Controller: 标识表现层组件
resource-pattern :过滤包
<context:include-filter> 子节点表示要包含的目标类
<context:exclude-filter> 子节点表示要排除在外的目标类
<context:component-scan> 下可以拥有若干个 <context:include-filter> 和 <context:exclude-filter> 子节点
A类引用B类,在B类上加 @Autowired (@Resource 、@Inject)
@Autowired 注解自动装配具有兼容类型的单个 Bean属性
构造器, 普通字段(即使是非 public), 一切具有参数的方法都可以应用@Authwired 注解
默认情况下, 所有使用 @Authwired 注解的属性都需要被设置. 当 Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false
默认情况下, 当 IOC 容器里存在多个类型兼容的 Bean 时, 通过类型的自动装配将无法工作. 此时可以在 @Qualifier 注解里提供 Bean 的名称. Spring 允许对方法的入参标注 @Qualifiter 已指定注入 Bean 的名称
@Authwired 注解也可以应用在数组类型的属性上, 此时 Spring 将会把所有匹配的 Bean 进行自动装配.
@Authwired 注解也可以应用在集合属性上, 此时 Spring 读取该集合的类型信息, 然后自动装配所有与之兼容的 Bean.
@Authwired 注解用在 java.util.Map 上时, 若该 Map 的键值为 String, 那么 Spring 将自动装配与之 Map 值类型兼容的 Bean, 此时 Bean 的名称作为键值
applicationContext.xml
该配置文件里需配置dataSource、transactionManager、sqlSessionFactory、MapperScannerConfigurer、所要用到的接口
2.web.xml配置文件
配置监听器,当web启动时,自动加载spring配置文件
配置DispatcherServlet,路由与controller的映射
3.servlet配置文件mvc-dispatcher-servlet.xml
配置基础扫描包(扫描controller),配置mvc:resources,配置视图解析器
4.写控制器
代码参见:http://blog.csdn.net/techbirds_bao/article/details/9233599/
OR M
O:对象,自己定义一个class,属性为private ,set与get方法
R:relational关系型数据库
M:mapping。*.hbm.cfg定义类与表的映射,变量与字段的映射,当没有column字段时,默认column=name
-----------------------
读取配置文件:(整个工程只初始化一次)
Configuration cfg=new Configuration();
cfg.configure("config.cfg.xml");
SessionFactory sessionFactory=cfg.buildSessionFactory();
--------------
模板代码
Session session=null;
Transaction tx=null;
try{
session=sessionFactory.openSession();
tx=session.beginTransaction();
..............代码
tx.commit();
}catch(Exception e){
if(tx!=null){
tx.rollback();
}
throw e;
}finally{
if(session!=null)session.close();
}
---------------------------------------------------------
在*.xml配置文件中设置bean
<bean id="helloWorld" class="spring.bean.HelloWorld">
<!-- 为属性赋值 ,通过set方法-->
<property name="name" value="zouhuiying"></property>
</bean>
<bean id="book" class="spring.bean.Constructor1">
<!--通过构造方法赋值-->
<constructor-arg value="math"></constructor-arg>
<constructor-arg value="180"></constructor-arg>
<!--通过index(构造方法中变量的索引)标识,或者type(变量的类型)-->
</bean>
![CDATA[]]特殊字符
<value></value>:
<bean id="book" class="spring.bean.Constructor1">
<constructor-arg>
<value>![CDATA[<math^>]]</value>
</constructor-arg>
</bean>
内部bean
<bean id="" class="">
<property name="">
<!--内部bean不能被外部使用,所以可以不加id属性-->
<bean class="">
<constructor-arg>
<value><![CDATA[<math>*]]></value>
</constructor-arg>
</bean>
</property>
</bean>
赋值为null:
<constructor-arg><null/></constructor-arg>
级联属性赋值(内部类的属性):属性需要先初始化后,才可以为级联属性赋值,否则处异常
-------------------------------------------------------
配置集合属性
list<car>
<property name="">
<list>
<ref bean="" />
</list>
</property>
------------
使配置map属性
<property name="">
<map>
<entry key="" value-ref="">
</entry>
</map>
</property>
-------------
<bean>
<property>
<props>
<prop key="">value</prop>
<prop key="">value</prop>
</props>
</property>
</bean>
-------------
配置单例的集合bean
<property name="car" ref="cars">
<util:list id="cars">
<ref bean="car1"/>
<ref bean="car2"/>
</util:list>
------------
p:命名空间
xmlns:p="http://www.springframework.org/schema/p"
<bean id="person2" class="spring.bean.Person" p:name="baobao" p:age="23" p:car-ref="car1" />
---------------
配置properties属性值
Properties类
private Properties properties;
<bean id ="datasource" class="spring.bean.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">zouhuiying</prop>
</props>
</property>
</bean>
-------------------------------------------------------
获取bean
首先获取IOC容器ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
即:加载配置文件,获取IOC容器后,系统会自动执行配置文件中bean的set方法和构造函数
HelloWorld hello=(HelloWorld) ctx.getBean(HelloWorld.class); //通过类名
Constructor1 con=(Constructor1) ctx.getBean("book2");//通过id
--------------------------------------------------------
自动装配
autowire=""
byName:set方法的名字bean的id //map bean不能自动装配
byType:class的类型。只能有一个bean
-----------------------------------------------------------
配置的继承parent parent="person"
abstract="True" 抽象bean不能被实例化
如果没有指定class,必须要设置abstract="True"
子类重写父类的property时,只能用父类已经用过的属性
bean直接的依赖
depends-on="某个bean的id
-----------------------------------------------------------
bean的作用域
使用<bean></bean>是默认是sscope=ingleton单例,每次调用都是同一个bean
设置scope属性prototype(原型)每次用bean时,都是产生一个新的bean,初始化IOC容器时,并不初始化该bean
-----------------------------------------------------------
使用外部属性文件
spring要连接数据库,需要配置dataSource bean,class="系统的datasource"
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
${var}
--------------------------------------------------------------
spring语言表达式:SpEL
#{...} //...代表SpEL,动态赋值
value="#{'gggggggg'}" //直接赋值
T():引用系统函数
#{T(java.lang.Math).PI * 80}
-----------------------------------------------------------------
bean生命周期
init-method="init" //自己写初始化和销毁方法
destroy-method="destroy"
ClassPathXmlApplicationContext对象有个close方法
ApplicationContext没有close放法
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beanss.xml");
Car car=(Car) ctx.getBean("car");
System.out.println(car);
ctx.close();
}
<bean id="car" class="spring.beans.Car" p:brand="Audi" p:color="red" init-method="init" destroy-method="destroy" />
bean的后置处理器:不需要配置id,IOC容器自动识别
MyBeanPostProcessor继承BeanPostProcessor接口,实现方法postProcessAfterInitialization与postProcessBeforeInitialization(Object bean, String beanName)
配置文件:<bean class="spring.beans.MyBeanPostProcessor" />
-----------------------------------------------------------------------------------
通过调用静态工厂方法创建 Bean
class Car
public class StaticFactory {
private static Map<String, Car> cars=new HashMap<String, Car>();
static{
cars.put("car1", new Car("house","red"));
cars.put("car2", new Car("audi","white"));
}
public static Car getCar(String name){
return cars.get(name); //调用的是car的工厂方法
}
}
配置文件:
<bean id="car1" class="springfactory.StaticFactory" factory-method="getCar">
<constructor-arg value="car1"></constructor-arg>
</bean>
main函数
ApplicationContext ctx=new ClassPathXmlApplicationContext("factory.xml");
Car car=(Car)ctx.getBean("car1");
System.out.println(car);
----------------------------
通过调用实例工厂方法创建 Bean
public class InstanceFactory {
private Map<String, Car> cars=null;
public InstanceFactory() {
super();
cars=new HashMap<String, Car>();
cars.put("one", new Car("ford","yellow"));
cars.put("two", new Car("anta","green"));
}
public Car getCar(String name) {
return cars.get(name);
}
配置文件:
<!-- 配置实例工厂 -->
<bean id="factory" class="springfactory.InstanceFactory"></bean>
<bean id="car2" factory-bean="factory" factory-method="getCar">
<constructor-arg value="two"></constructor-arg>
</bean>
------------------------------------------------------------------
通过FactoryBean配置bean
-------------------------------------------------------------------
配置扫描包,多个包用“,”分隔
<context:component-scan base-package="com.zou.service"></context:component-scan>
@Component: 基本注解, 标识了一个受 Spring 管理的组件
@Respository: 标识持久层组件
@Service: 标识服务层(业务层)组件
@Controller: 标识表现层组件
resource-pattern :过滤包
<context:include-filter> 子节点表示要包含的目标类
<context:exclude-filter> 子节点表示要排除在外的目标类
<context:component-scan> 下可以拥有若干个 <context:include-filter> 和 <context:exclude-filter> 子节点
A类引用B类,在B类上加 @Autowired (@Resource 、@Inject)
@Autowired 注解自动装配具有兼容类型的单个 Bean属性
构造器, 普通字段(即使是非 public), 一切具有参数的方法都可以应用@Authwired 注解
默认情况下, 所有使用 @Authwired 注解的属性都需要被设置. 当 Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false
默认情况下, 当 IOC 容器里存在多个类型兼容的 Bean 时, 通过类型的自动装配将无法工作. 此时可以在 @Qualifier 注解里提供 Bean 的名称. Spring 允许对方法的入参标注 @Qualifiter 已指定注入 Bean 的名称
@Authwired 注解也可以应用在数组类型的属性上, 此时 Spring 将会把所有匹配的 Bean 进行自动装配.
@Authwired 注解也可以应用在集合属性上, 此时 Spring 读取该集合的类型信息, 然后自动装配所有与之兼容的 Bean.
@Authwired 注解用在 java.util.Map 上时, 若该 Map 的键值为 String, 那么 Spring 将自动装配与之 Map 值类型兼容的 Bean, 此时 Bean 的名称作为键值
发表评论
-
mybatis动态sql语句
2016-05-07 18:09 451方法传过来的是list参数,list里面是对象 void s ... -
springmvc获取js文件失败
2016-05-03 19:18 450在springmvc的配置文件中加入 <mvc:reso ... -
menu模块的自关联查询
2016-04-27 10:16 622<!-- 针对菜单的父子关系,定义的树状结构Result ... -
spring配置文件快速查找
2016-04-12 21:20 373<!--spring 扫包 @Service ... ... -
mybatis配置文件
2016-04-12 21:11 321<bean id="sqlSessionFac ... -
mybatis实战
2016-04-06 11:23 403http://blog.csdn.net/techbirds_ ... -
spring 泛型
2016-04-05 13:23 449Spring4.0可以为子类注入子类的泛型类型,成员变量的引用 ... -
spring基础学习笔记
2016-03-31 13:58 376OR M O:对象,自己定义一个class,属性为privat ... -
hibernate 事务
2016-03-07 11:10 4401.新建java(maven)普通工程 2.配置maven, ... -
spring jdbc 事务
2016-03-04 18:04 472使用事务 一.配置文件 ... -
spring hibernate
2016-03-04 10:31 3201. Spring 整合 Hibernate 整合什么 ? ... -
spring mvc参数传递
2016-03-02 20:50 5391.使用HttpServletRequest request ... -
spring mvc 处理器映射的三种方式
2016-03-02 15:04 6291.通过beanname 配置springmvc-servl ... -
spring mvc 注解实例
2016-03-02 11:29 4041.web.xml <?xml version=&qu ... -
spring mvc 完整实例
2016-03-01 19:50 5851.新建工程,把spring所需的包copy到工程里 2.配置 ... -
spring jdbc完整实例
2016-02-29 21:36 379StudentDAO.java package com.jd ... -
spring aop 完整实例
2016-02-29 16:10 394Employe.java package com.aop; ...
相关推荐
Spring MVC整合Mybatis示例,该文本与http://blog.csdn.net/geloin/article/details/7536968配套。
Spring mvc整合mybatis例子
spring mvc 整合mybatis实例,spring mvc 整合mybatis实例
spring mvc整合mybatis例子
第4篇是Spring MVC与MyBatis的项目整合实战,通过对水果网络销售平台的需求分析、功能设计、数据库设计以及模块详细编码实现,让读者了解整合项目开发的整体流程。 《Spring MVC+MyBatis开发从入门到项目实战》对...
SSM(spring+spring MVC+mybatis)开发学生信息后台管理系统,实现学生增删改查功能设计一个简单的学生信息管理系统,要求使用SSM框架技术整合实现,用户登录后能够通过Web页面添加、删除、修改和查询学生信息 ...
基于spring+spring mvc+mybatis框架整合实现超市货物管理系统(实现登入功能、mysql数据库的增删改查及分页显示)
maven、spring、spring mvc、mybatis 整合实现ssm通用增删改查基础开发框架.maven、spring、spring mvc、mybatis 整合实现ssm通用增删改查基础开发框架.maven、spring、spring mvc、mybatis 整合实现ssm通用增删改查...
本例使用Spring,SpringMVC,MyBatis技术进行框架整合,代码都有详细的注释和说明,初学SSM框架很好用,特此推荐。
Java EE 框架整合开发⼊⻔到实战——Spring+Spring MVC+MyBatis(微课版)课后习题答案.pdf
Spring + Spring MVC + MyBatis整合项目的源码。
包含课设要求所有资源 基于Spring + Spring MVC + MyBatis的图书馆管理系统,使用Maven进行包管理。主要功能包括:图书查询、图书管理、图书编辑、读者管理、图书的借阅与归还以及借还日志记录等。
spring+spring mvc+mybatis
SSM框架的配置搭建 spring,spring mvc,mybatis 整合详解
Spring MVC + Mybatis+Spring实现的个人博客系统基于SSM实现的个人博客系统.zip
全套完整的spring+spring mvc+mybatis+mysql+dubbo整合开发任务流程后台管理系统
spring mvc mybatis 整合源码,带数据库脚本,带详细注释,适合初、中、高各级开发人员使用
本项目实用Spring + Spring MVC + Mybatis。数据库实用Mysql数据库 项目主要涉及,SSM框架的配置搭建,涉及Mybatis一对多的插入和查询,同时也涉及到一些简单的文件上传和下载.
SSM框架的配置搭建 spring、 spring mvc、 mybatis 整合详解
spring mvc、mybatis、bootstrap、shiro 框架整合开发网上求职招聘毕业设计系统、带mysql数据库