mybatis入门(1)

得到sqlSession工厂,用sqlSession工厂创建出sqlSession。

配置方式一

(太麻烦,已废弃不用了)

 

mybatis.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>

    <!--加载数据库配置文件-->
    <properties resource="db.properties"/>

    <!--配置环境,特别是数据源-->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${db.driver}"/>
                <property name="url" value="${db.url}"/>
                <property name="username" value="${db.username}"/>
                <property name="password" value="${db.password}"/>
            </dataSource>
        </environment>
    </environments>

    <!--引入mybaties的mapper.xml文件-->
    <mappers>
        <mapper resource="studentMapper.xml"/>
    </mappers>
</configuration>

StudentMapper.java

<?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="test">
    <!--1.查询所有的学生-->
    <select id="queryList" resultType="com.csdn.pojo.Student">
     select * from student
    </select>
</mapper>

 

MybatisUtil.java
package com.csdn.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import java.io.IOException;
import java.io.InputStream;

public class MybatisUtil {

    /**
     * 得到mysql的session工厂
     * @param file
     * @return
     */
    public static SqlSessionFactory getSqlSessionFactory(String file){
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(file);
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
            return factory;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过工厂得到session。可以设置是否自动提交事务。默认为自动提交
     * @param isAutoCommit
     * @return
     */
    public static SqlSession getSqlSession(boolean isAutoCommit){
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory("mybatis.xml");
        return sqlSessionFactory.openSession(isAutoCommit);
    }

}

Test.java

package com.csdn.test;

import com.csdn.pojo.Student;
import com.csdn.service.StudentService;
import com.csdn.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath:spring.xml")
public class Test {

//    StudentService studentService;

    @org.junit.Test
    public void test(){
//        得到sqlsession,然后再进行查询
        SqlSession sqlSession = MybatisUtil.getSqlSession(true);
        List<Student> list = sqlSession.selectList("test.queryList");
        System.out.println(list);
    }
}

 

配置方式二

(普遍使用)

与spring整合,xml配置文件只需要这两个

spring.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:component-scan base-package="com.csdn"/>

    <import resource="classpath*:spring-mybatis.xml"/>
</beans>

spring.mybatis.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*:db.properties"/>

    <!--配置数据源-->
    <bean id="dataSources" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${db.driver}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>

    <!--session工厂-->
    <bean id = "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--数据源-->
        <property name="dataSource" ref="dataSources"/>
        <!--相当于dao的实现类-->
        <property name="mapperLocations" value="classpath*:mapper/*.xml"/>
        <!--配置别名。配置了可不用写全名-->
        <property name="typeAliasesPackage" value="com.csdn.pojo"/>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--配置dao接口包-->
        <property name="basePackage" value="com.csdn.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
</beans>

studentMapper.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.csdn.mapper.StudentDao">
    <!--1.查询所有的学生-->
    <!--查出的数据库字段与实体类中属性一一对应(名字一样就能自动对应上)-->
    <select id="queryList" resultType="com.csdn.pojo.Student">
     select * from student
    </select>
</mapper>

Test2.java

package com.csdn.test;

import com.csdn.pojo.Student;
import com.csdn.service.StudentService;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class Test2 {
    @Autowired
    StudentService studentService;

    @org.junit.Test
    public void test(){
        List<Student> students = studentService.queryList();
        System.out.println(students);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值