Spring 集成Mybatis

        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&amp;zeroDateTimeBehavior=convertToNull&amp;noAccessToProcedureBodies=true
username=root
password=123456

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值