mybatis与spring整合(基于Annotation)
来源:互联网 发布:什么软件可以测手速 编辑:程序博客网 时间:2024/06/07 03:17
本文主要介绍了如何将mybatis和spring整合在一起使用,本人使用的是mybatis3.05 + spring3.1.0M2 ,使用dbcp作为数据库连接池。
1.编写数据访问接口(UserDao.java)
package com.mybatis;
import org.apache.ibatis.annotations.Select;
public interface UserDao {
@Select("select count(*) c from user;")
public int countAll();
}
2.编写服务层接口代码(UserService.java)
package com.mybatis;
public interface UserService {
public int countAll();
}
3.编写服务层实现代码(UserServiceImpl.java)
package com.mybatis;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public int countAll() {
return this.userDao.countAll();
}
}
4.编写Spring配置文件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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/hlp?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"></property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.mybatis.UserDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="userService" class="com.mybatis.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
5.编写测试代码
package com.mybatis;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserServiceTest {
@Test
public void userServiceTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService)context.getBean("userService");
System.out.println(userService.countAll());
}
}
附录:需要导入的库
- mybatis与spring整合(基于Annotation)
- mybatis与spring整合(基于Annotation)
- mybatis与spring整合(基于Annotation)
- mybatis与spring整合(基于Annotation)
- mybatis与spring整合(基于配置文件)
- mybatis与spring整合(基于配置文件)
- mybatis与spring整合(基于配置文件)
- mybatis与spring整合(基于配置文件)
- mybatis与spring整合(基于配置文件)
- mybatis与spring整合(基于配置文件)
- mybatis与spring整合(基于配置文件)
- mybatis与spring整合(基于配置文件)
- mybatis与spring整合(基于配置文件)
- mybatis与spring整合(基于配置文件)
- 基于Annotation的mabatis与Spring整合
- spring与mybatis整合
- 整合spring与myBatis
- mybatis 与 spring 整合
- 用nagios监控ORACLE服务器
- 一个很好的博客
- Spring AOP 拦截
- 灰盒测试概念,摘自百度
- 注册表学习——创建快捷方式时不显示“快捷方式”文字
- mybatis与spring整合(基于Annotation)
- Spring工作原理及其作用
- HDOJ P1015 Safecracker
- windows row socket
- 框架学习之Spring 第一节 认识Spring以及环境的配置
- 框架学习之Spring 第二节 采用Spring管理Bean和依赖注入
- Android推送方案分析(MQTT/XMPP/GCM)
- 框架学习之Spring 第四节 Spring集成JDBC组件开发
- asp.net页面传值的几种方式