MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。贴代码:
一、引用的jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
二、pom文件还需新增
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
二、Dao层、Service层、mapper文件、Controller层
package OThinker.H3.seedland.dao;
import OThinker.H3.seedland.pojo.Test;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface IUserInfoDao {
public Test getUserById(@Param("id") String id);
}
package OThinker.H3.seedland.service;
import OThinker.H3.seedland.pojo.Test;
public interface IUserInfoService {
public Test getById();
}
package OThinker.H3.seedland.service.impl;
import OThinker.H3.seedland.dao.IUserInfoDao;
import OThinker.H3.seedland.pojo.Test;
import OThinker.H3.seedland.service.IUserInfoService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @Author laixh
* @Description //TODO
* @Date 2019
**/
@Service("userServie")
public class UserInfoServiceImpl implements IUserInfoService {
@Resource
IUserInfoDao userInfoDao;
@Override
public Test getById() {
return userInfoDao.getUserById("");
}
}
TestMapper.xml,这个文件是声明Dao之间的联系。
<?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="OThinker.H3.seedland.dao.IUserInfoDao">
<select id="getUserById" resultType="OThinker.H3.seedland.pojo.Test" parameterType="String">
SELECT COUNT(*) cnt FROM ot_user;
</select>
</mapper>
package OThinker.H3.Controller.Seedland.Controllers;
import OThinker.H3.seedland.pojo.Test;
import OThinker.H3.seedland.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author laixh
* @Description //TODO
* @Date 2019
**/
@RequestMapping("/Portal/test")
@RestController
public class SeedlandTestController {
@Autowired
IUserInfoService userServie;
@RequestMapping("/say")
public String getUser(){
Test test=userServie.getById();
return test.getCnt()+"";
}
}
三、配置文件
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.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
default-lazy-init="false">
<!-- 引入mybatis配置文件-->
<import resource="classpath:config/mybatis-config.xml"/>
<context:annotation-config />
<!-- 扫描包-->
<context:component-scan base-package="OThinker.H3.seedland.*">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames" value="config/messages/message"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="propertyConfigurer" class="com.h3bpm.base.spring.PropertyConfigurer">
<property name="location">
<value>classpath:/config/h3bpm_portal_app.properties</value>
</property>
<property name="fileEncoding" value="UTF-8"/>
</bean>
</beans>
mybatis-config.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 引入占位符-->
<context:property-placeholder location="classpath*:/config/datasource.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="true" />
<property name="testWhileIdle" value="true" />
<property name="maxIdle" value="30" />
<property name="maxActive" value="50" />
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="60"/>
<property name="logAbandoned" value="true"/>
</bean>
<!--扫描加载mapper文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath*:OThinker/H3/seedland/mapping/*Mapper.xml"/>
</bean>
<!--扫描加载mapper文件与之对应的接口dao -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="OThinker.H3.seedland.dao"/>
</bean>
</beans>
数据库连接配置文件datasource.properties
url=jdbc:mysql://127.0.0.1:3306/h3bpm?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&noAccessToProcedureBodies=true
username=root
password=123456
本文介绍如何在Spring框架中整合MyBatis,包括配置数据源、定义DAO接口及实现、编写Service层和服务实现,同时展示了Controller层的简单示例。
1537

被折叠的 条评论
为什么被折叠?



