java中的接口是类吗
235
2023-05-23
spring与mybatis三种整合方法
1、采用MapperScannerConfigurer,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBean。
spring-mybatis.xml:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p" xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.1.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.1.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-3.1.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-3.1.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc-4.0.xsd">
UserMapper.xml:
select * from t_user where id=#{id}
dao类:
/**
* 这里的@MapperScan就是上面所讲的Mapper扫描器中所需要的配置,会自动生成代理对象。
* 注意,接口中的方法名称要和对应的MyBatis映射文件中的语句的id值一样,因为生成的
* 动态代理,会根据这个匹配相应的Sql语句执行。另外就是方法的参数和返回值也需要注
* 意。接口中的方法如何定义,对应的MyBatis映射文件就应该进行相应的定义。
* 最后,标注中的userDao是用来作为Spring的Bean的id(或name)进行使用的,方便我
* 们在Service层进行注入使用。
*/
@MapperScan
public interface UserDao {
//此处的方法名必须和mapper中的映射文件中的id同名
//回去映射文件中通过com.hua.saf.dao.UserDao.getUser,即this.getClass().getName()+".getUser"
public User getUser(int id);
}
service类
@Service("userService")
public class UserServiceImpl implements IUserService {
@Resource
private UserDao userDao;
public User getUser(int id) {
return userDao.getUser(id);
}
}
2、采用接口org.apache.ibatis.session.SqlSession的实现类org.mybatis.spring.SqlSessionTemplate。
mybatis中, sessionFactory可由SqlSessionFactoryBuilder.来创建。MyBatis-Spring 中,使用了SqlSessionFactoryBean来替代。SqlSessionFactoryBean有一个必须属性dataSource,另外其还有一个通用属性configLocation(用来指定mybatis的xml配置文件路径)。
spring-mybatis.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p" xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.1.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.1.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-3.1.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-3.1.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc-4.0.xsd">
sqlMapConfig.xml
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
User.java
public class User {
private int id;
private String username;
private String password;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
UserDao.java
@Repository
public class UserDao{
@Resource
private SqlSessionTemplate sqlSessionTemplate;
public User getUser(int id) {
return sqlSessionTemplate.selectOne(this.getClass().getName() + ".getUser", 1);
}
}
UserService.java
@Service
public class UserService{
@Resource
private UserDao userDao;
public User getUser(int id) {
return userDao.getUser(id);
}
}
3、采用抽象类org.mybatis.spring.support.SqlSessionDaoSupport提供SqlSession。
spring-mybatis.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p" xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.1.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.1.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <property name="maxWait" value="${maxWait}" />
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-3.1.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-3.1.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<property name="maxWait" value="${maxWait}" />
sqlMapConfig.xml
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
User.java
public class User {
private int id;
private String username;
private String password;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
UserDao.java
@Repository
public class UserDao extends SqlSessionDaoSupport{
public User getUser(int id) {
return this.getSqlSession().selectOne(this.getClass().getName() + ".getUser", 1);
}
//使用SqlSessionDaoSupport必须注意,此处源码1.1.1中有自动注入,1.2中取消了自动注入,需要手工注入,侵入性强
//也可在spring-mybatis.xml中如下配置,但是这种有多少个dao就要配置到少个,多个dao就很麻烦。
//
//
//
@Resource
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
// TODO Auto-generated method stub
super.setSqlSessionFactory(sqlSessionFactory);
}
}
UserService.java
@Service
public class UserService{
@Resource
private UserDao userDao;
public User getUserss(int id) {
return userDao.getUser(1);
}
}
以上所述是给大家介绍的基于spring与mybatis三种整合方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~