Spring3.0+ibatis+Oracle全注解开发(详细过程)
//注解使用说明
看字面含义,很容易却别出其中三个:
@Controller 控制层,就是我们的action层
@Service 业务逻辑层,就是我们的service或者manager层
@Repository 持久层,就是我们常说的DAO层
而@Component (字面意思就是组件),它在你确定不了事哪一个层的时候使用。
其实,这四个注解的效果都是一样的,Spring都会把它们当做需要注入的Bean加载在上下文中;
但是在项目中,却建议你严格按照除Componen的其余三个注解的含义使用在项目中。这对分层结构的web架构很有好处!!
1 创建一个web项目,相应的结构目录
2 右击项目(MyEclipase-->Add Spring..)添加spring3.0框架 加入相应的jar包,
使用包如下:
3 添加Spring3.0后会自动生成applicationContext.xml文件
4 编写连接数据库配置文件jdbc.properties
jdbc.properties
4:SqlMapConfig.xml文件总文件,用户寻找数据库操作语句
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings useStatementNamespaces="true" /> <!--在这里设置是否使用命名空间必须要设置 -->
<!--映射文件 -->
<sqlMap resource="com/zte/dao/avtive.xml"/>
</sqlMapConfig>
-------------------------------------------------------------------------------------------------------------------------------------------------------------
avtive.xml
-------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="active1">
<select id="queryall" resultClass="com.zte.bean.Activeinfo">
<![CDATA[
select c.actid,
c.activetitle,
c.pname,
c.pmonery,
to_char(c.actstartime,'yyyy-mm-dd hh24:mi:ss') actstartime,
to_char(c.actendtime,'yyyy-mm-dd hh24:mi:ss') actendtime,
c.createtime,
c.pnum,
c.gressmin,
c.gressmax,
c.operid,
c.actstatus,
c.des
from mandb.t12580_callgress_active c
]]></select>
<select id="querybyid" resultClass="com.zte.bean.Activeinfo" parameterClass="int">
<![CDATA[
select c.actid,
c.activetitle,
c.pname,
c.pmonery,
to_char(c.actstartime,'yyyy-mm-dd hh24:mi:ss') actstartime,
to_char(c.actendtime,'yyyy-mm-dd hh24:mi:ss') actendtime,
c.createtime,
c.pnum,
c.gressmin,
c.gressmax,
c.operid,
c.actstatus,
c.des
from mandb.t12580_callgress_active c where c.actid=#actId#
]]></select>
</sqlMap>
----------------------------------------------------------
4 编写applicationContext.xml配置
applicationContext.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:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 启用注解 -->
<context:annotation-config/>
<!-- 注解使用范围 -->
<context:component-scan base-package="com.zte.*" />
<!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- 读取配置.properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 数据库配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!--连接池启动时的初始值 -->
<property name="initialSize" value="${initialSize}"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="${maxActive}"/>
<!--最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${maxIdle}"/>
<!--最小空闲值当空闲的连接数少于阀值时,连接池就会预申请去一些连 接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${minIdle}"/>
</bean>
<!-- spring整合ibatis -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml"/> <!--读取配置文件-->
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<!-- 事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
<alias alias="transactionManager" name="transactionManager" />
<bean id="TxTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager" />
<property name="timeout" value="30" />
</bean>
</beans>
----------------------------------------------------------------------------------------------
5 在web.xml中配置spring监听
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!--spring配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
二 配置文件都配置好了,现在开始编写代码
1 编写一个实体文件,该文件时段最好与数据库表字段一致,便于区辨
Activeinfo.java
------------------------------------------------------
package com.zte.bean;
/**
* <p>文件名称: Activeinfo.java </p>
* <p>文件描述: 活动信息</p>
* <p>版权所有: 版权所有(C)2012-2013</p>
* <p>公 司: xxxxxx</p>
* <p>内容摘要: </p>
* <p>其他说明: </p>
* <p>完成日期:2008-11-11</p>
* <p>修改记录1: </p>
* <pre>
* 修改日期:
* 版 本 号:
* 修 改 人:
* 修改内容:
* </pre>
* @version 1.0
* @author lfz
*/
public class Activeinfo {
private String actid ; //活动编号
private String activetitle; //活动标题
private String pname; //活动奖品名称
private String pmonery; // 奖品预算成本
private String actstartime; // 活动开始时间
private String actendtime; // 活动结束时间
private String createtime; // 创建时间
private String pnum ; // 奖品数量
private String gressmin ; // 最小金额
private String gressmax ; // 最大金额
private String operid ; // 操作工号
private String actstatus ; //活动状态 1启用,2禁用
private String des ; // 活动描述
public String getActid() {
return actid;
}
public void setActid(String actid) {
this.actid = actid;
}
public String getActivetitle() {
return activetitle;
}
public void setActivetitle(String activetitle) {
this.activetitle = activetitle;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPmonery() {
return pmonery;
}
public void setPmonery(String pmonery) {
this.pmonery = pmonery;
}
public String getActstartime() {
return actstartime;
}
public void setActstartime(String actstartime) {
this.actstartime = actstartime;
}
public String getActendtime() {
return actendtime;
}
public void setActendtime(String actendtime) {
this.actendtime = actendtime;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getPnum() {
return pnum;
}
public void setPnum(String pnum) {
this.pnum = pnum;
}
public String getGressmin() {
return gressmin;
}
public void setGressmin(String gressmin) {
this.gressmin = gressmin;
}
public String getGressmax() {
return gressmax;
}
public void setGressmax(String gressmax) {
this.gressmax = gressmax;
}
public String getOperid() {
return operid;
}
public void setOperid(String operid) {
this.operid = operid;
}
public String getActstatus() {
return actstatus;
}
public void setActstatus(String actstatus) {
this.actstatus = actstatus;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
}
----------------------------------------------------------------
2 编写持久层(即Dao层)接口
ActiveDao.java
-----------------------------------------
package com.zte.dao;
import java.util.List;
import com.zte.bean.Activeinfo;
public interface ActiveDao {
/**
* 获取所有活动信息
* @return
*/
public List<Activeinfo> getAll();
/**
* 根据活动编号获取信息
* @param actid
* @return
*/
public Activeinfo getInfoById(Integer actId);
}
-------------------------------------------------------------------------------------------------
编写实现类
ActiveDaoImpl.java
package com.zte.dao.impl;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zte.bean.Activeinfo;
import com.zte.dao.ActiveDao;
import com.zte.service.ActiveService;
@Repository("activeDao")//持久层,次名字用户注入该累
@Transactional
public class ActiveDaoImpl implements ActiveDao {
//@Qualifier("sqlMapClientTemplate") //注释指定注入 Bean 等同与===@Resource(name="sqlMapClientTemplate")
@Resource(name="sqlMapClientTemplate") //sqlMapClientTemplate次名字是applicationContext.xml是配置的bean
private SqlMapClientTemplate sqlMapClientTemplate;
@SuppressWarnings("unchecked")
public List<Activeinfo> getAll() {
List<Activeinfo> list = new ArrayList<Activeinfo>();
list = sqlMapClientTemplate.queryForList("active1.queryall");
return list;
}
@SuppressWarnings("unchecked")
public Activeinfo getInfoById(Integer actId) {
Activeinfo in=new Activeinfo();
List<Activeinfo> list=sqlMapClientTemplate.queryForList("active1.querybyid", actId);
if(null!=list && list.size()>0){
in=list.get(0);
}
return in;
}
}
编写业务类
ActiveService.java
----------------------------------------------------------------------------------------
package com.zte.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.zte.bean.Activeinfo;
import com.zte.dao.ActiveDao;
@Service("activeService")//业务层
public class ActiveService {
// @Autowired
// @Qualifier("activeDao") //注释指定注入 Bean = @Resource(name= "activeDao")
@Resource(name= "activeDao")
public ActiveDao activedao;
public List<Activeinfo> getAll(){
List<Activeinfo> allinfoActiveinfos=activedao.getAll();
return allinfoActiveinfos;
}
}
------------------------------------------------------
6编写测试
BTset.java
------------------------------
package com.zte.test;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zte.bean.Activeinfo;
import com.zte.service.ActiveService;
public class BTset {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void test() {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
ActiveService jS=(ActiveService)ctx.getBean("activeService");
List<Activeinfo> list = jS.getAll();
for (Activeinfo activeinfo : list) {
System.out.println(activeinfo.getActivetitle());
}
Activeinfo inActiveinfo = jS.getById(4);
System.out.println("jjjj"+inActiveinfo.getDes());
}
}