您的足迹:首页 > JAVA >spring整合mybatis

spring整合mybatis

spring中整合mybatis,首先必须在pom.xml中引入的是mybatis的包


<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>


然后在spring的配置文件中添加下面配置文件

配置的主要功能是,让datesource注入sqlsession中和自动扫描com.ljtxh.enterprise.repository.mybatis包下所有MyBatisRepository注解过的dao



<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描entity目录,省掉Configuration.xml里的手工配置 -->
<property name="typeAliasesPackage" value="com.ljtxh.enterprise.entity.mybatis" />
<!-- 显式指定Mapper文件位置 -->
<property name="mapperLocations" value="classpath:/mybatis/*Mapper.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ljtxh.enterprise.repository.mybatis" />
<property name="annotationClass" value="com.ljtxh.enterprise.repository.mybatis.MyBatisRepository" />
</bean>


dao类的内容


package com.ljtxh.enterprise.repository.mybatis;

import java.util.List;
import java.util.Map;

import com.ljtxh.enterprise.entity.mybatis.ArticleMyBatis;

@MyBatisRepository
public interface ArticleMyBatisDao {
public List<Map> findAll();
public ArticleMyBatis findOne(long id);
}

Mapper文件内容



<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ljtxh.enterprise.repository.mybatis.ArticleMyBatisDao" >
    
  <select id="findAll"  resultType="map">
select * from article
</select>
<select id="findOne" parameterType="int" resultType="com.ljtxh.enterprise.entity.mybatis.ArticleMyBatis">
select * from article where id=#{id}
</select>
</mapper>

entity内容



package com.ljtxh.enterprise.entity.mybatis;

import java.util.Date;

public class ArticleMyBatis {
private Integer id;
private Integer catId;
private String title;
private String defined;
private String content;
private String image;
private String keywords;
private Date addTime;
private Short click;
private String description;
private String homeSort;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getCatId() {
return catId;
}
public void setCatId(Integer catId) {
this.catId = catId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDefined() {
return defined;
}
public void setDefined(String defined) {
this.defined = defined;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getKeywords() {
return keywords;
}
public void setKeywords(String keywords) {
this.keywords = keywords;
}
public Date getAddTime() {
return addTime;
}
public void setAddTime(Date addTime) {
this.addTime = addTime;
}
public Short getClick() {
return click;
}
public void setClick(Short click) {
this.click = click;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getHomeSort() {
return homeSort;
}
public void setHomeSort(String homeSort) {
this.homeSort = homeSort;
}

}

service 内容



package com.ljtxh.enterprise.service.mybatis;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ljtxh.enterprise.entity.mybatis.ArticleMyBatis;
import com.ljtxh.enterprise.repository.mybatis.ArticleMyBatisDao;


@Component
public class ArticleMyBatisService {
@Autowired
ArticleMyBatisDao articleMyBatisDao;
public  List<Map> findAll(){
return articleMyBatisDao.findAll();
}
public ArticleMyBatis findOne(Long id) {
// TODO Auto-generated method stub
return articleMyBatisDao.findOne(id);
};
}


controller内容


package com.ljtxh.enterprise.web.front;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ljtxh.enterprise.entity.mybatis.ArticleMyBatis;
import com.ljtxh.enterprise.service.mybatis.ArticleMyBatisService;

@Controller
@RequestMapping("articleMyBatis")
public class ArticleMyBatisController {
@Autowired
private ArticleMyBatisService articleMyBatisService;
@RequestMapping
@ResponseBody
   public List<Map> findAll(){
  return articleMyBatisService.findAll();
   }
}


运行结果

QQ图片20151206135140.png


本博客所有文章如无特别注明均为原创。作者:小天复制或转载请以超链接形式注明转自 钟天才的博客
原文地址《spring整合mybatis

相关推荐

网友评论(0)