spring整合Mybatis oracle数据库连接配置(dbcp)

Spring + struts + MyBatis 结合编码demo

一.首先导入相应的 Spring + struts + MyBatis jar包

 1.Spring + MyBatis :如下操作顺序

1.1:首先创建数据库连接:(oracle)


<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="数据库用户名" />
<property name="password" value="数据库用户名密码" />

</bean>


1.2:获取sqlSession 对象:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定连接资源 -->
<property name="dataSource" ref="myDataSource" />
<!--指定映射文件 -->
<property name="mapperLocations" value="classpath:org/great/sqlxml/SqlMapperUser.xml" />
</bean>

1.3: 获取接口类:有两种方法获取 ,第一种手动添加获取,第二种自动扫描获取,(自动比较方便)

<!-- 手动添加接口的方式,通过ID 获取每个sql对应的接口类 -->
 <bean id="stuMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property> <property 
name="mapperInterface" value="org.great.interfaces.EmpetyInterface"></property> 
</bean>


<!-- 自动扫描各个Mapper接口,并注册对应的MapperFactoryBean对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.great.interfaces" />
</bean>


到这里 Spring + MyBatis 的配置就都配置好了,接下来就是测试调用:

ApplicationContext apps = new ClassPathXmlApplicationContext("applicationcontext.xml");//获取applicationcontext 对象并解析 applicationcontext.xml 

/*这种是自动扫描接口的方式获取接口,getBean(“这里写接口类的类名 首字母改成小写就ok”) 如果是手动添加的话:getBean(“bean 的ID”) 

如:getbean("stuMapper");*/
EmpetyInterface ei = (EmpetyInterface)apps.getBean("empetyInterface");

二:spring + Struts

1. 配置struts.xml 在Struts基础上加上框架的核心配置: 服务器启动时,通过监听器初始化Spring的配置环境 监听器,默认加载文件是 applicationContext.xml

<!-- spring+struts 要配置实例spring -->
  <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationcontext.xml</param-value>
</context-param>

<!-- 过滤器 -->
<filter>
  <filter-name >struts2 </filter-name >  
  <filter-class>  
      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
    </filter-class>  
</filter>   
<filter-mapping>  
 <filter-name >struts2 </filter-name >  
 <url-pattern >/*</url-pattern >  
</filter-mapping>

2.配置applicationContext.xml 

<!-- struts2与Spring 整合 -->
 <context:component-scan base-package="org.great.action" />
<!-- 利用依赖注入,class:对应action的地址,scope="prototype": 因为默认是单例的所以这里要改成多例状态-->
<bean id="loginAction" class="org.great.action.LoginAction" autowire="byName" scope="prototype">
</bean>

3.在struts.xml 中设置:其中 class:struts2时的写法是:包名+类名,加入spring后:必须是applicationContext.xml 中bean 的 id,

<action name="loginaction" method="login" class="loginAction">

4.在action类中的写法:

//通过扫描注解来实例对象
@Controller
public class LoginAction {
//前端页面注入的对象
private Teachers teacher;
/*接口类的声明,如果是手动添加的那么属性名要与applicationContext.xml中的id名一致
* 如果是自动扫描的话:属性名只要是类名首字母小写就可以
*/
@Resource
private EmpetyInterface empetyInterface;

public String login(){

//调用接口下的方法
List<Teachers> li = empetyInterface.selAllForTest();

}


到这来就实现了 Spring + struts + MyBatis 的结合做法了。

猜你喜欢

转载自blog.csdn.net/dbsjack/article/details/65041517