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 的结合做法了。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 添加依赖 在pom.xml文件中添加MyBatisOracle数据库的依赖: ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <version>19.3.0.0</version> </dependency> ``` 2. 配置数据源 在application.properties或application.yml中配置数据源: ``` spring.datasource.url=jdbc:oracle:thin:@localhost:1521:ORCL spring.datasource.username=yourusername spring.datasource.password=yourpassword spring.datasource.driver-class-name=com.oracle.jdbc.Driver ``` 3. 配置MyBatis 在application.properties或application.yml中配置MyBatis: ``` mybatis.mapper-locations=classpath:/mapper/*.xml mybatis.type-aliases-package=com.example.demo.entity ``` 4. 创建实体类 创建对应Oracle表的实体类,注意实体类属性名要与表中字段名一致。 ``` public class User { private Long id; private String username; private String password; // getter and setter } ``` 5. 创建Mapper接口 创建UserMapper接口,并添加相应的SQL语句注解。 ``` @Mapper public interface UserMapper { @Select("SELECT * FROM user WHERE id = #{id}") User findById(Long id); // 其他查询/增加/更新/删除方法 } ``` 6. 测试 在Controller中注入UserMapper,并测试查询方法。 ``` @RestController public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/user/{id}") public User getUserById(@PathVariable Long id) { return userMapper.findById(id); } } ``` 7. 运行 启动Spring Boot应用,并访问http://localhost:8080/user/{id},测试查询方法是否生效。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值