MyBatis应用

MyBatis使用结构

java目录下:com.example.dao.TestMapper.java接口

public interface TestMapper{
	Pojo selectPojoById(Integer id);
}

resources目录下:com.example.dao.TestMapper.xml配置文件

<?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.example.dao.TestMapper">
    <select id="selectPojoById" resultType="student" parameterType="java.lang.Integer">
    	select * from pojo where id = #{id}
    </select>
</mapper>

MyBatis独立使用

全局配置文件MyBatisConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<!--读取jdbc.properties-->
    <properties resource="jdbc.properties"/>
    <settings>
    <!--配置log4j日志-->
        <setting name="logImpl" value="log4j"/>
    </settings>
    <typeAliases>
    <!--实体类注册别名-->
        <package name="com.example.pojo"/>
    </typeAliases>
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
    <!--扫描Mapper.xml文件-->
        <package name="com.example.dao">
    </mappers>
</configuration>

java目录下:com.example.dao.impl.TestMapperImpl.java实现类

public class TestMapperImpl implements TestMapper{
	private SqlSession sqlSession;

	@Override
	public Pojo selectPojoById(Integer id){
		Pojo pojo;
		sqlSession = this.open();
        pojo = sqlSession.selectOne("TestMapper.selectPojoById",id);
        this.close();
        return pojo;
	}
	//SqlSession开启方法
	private SqlSession open(){
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(resourceAsStream);
            sqlSession = sqlSessionFactory.openSession(true);
            resourceAsStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sqlSession;
    }
	//SqlSession关闭方法
    private void close(){
        if (sqlSession != null){
            sqlSession.close();
        }
    }
}

Spring集成MyBatis

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"
       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">
    <!--读取jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--扫描包里除被@Controller标记的类-->
    <context:component-scan base-package="com.example">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--配置alibaba druid数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--配置dao层接口与xml的映射-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.example.dao"/>
    </bean>
    <!--配置mybaties的SqlSession工厂方法的映射-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.example.pojo"/>
    </bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值