Mybaits课堂练习

a 夏天 提交于 2019-12-01 22:00:58
<?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="cn.kgc.dao.UserMapper">
    <select id="count" resultType="int">
        select count(1) from smbms_user
    </select>
    <select id="userlist" resultType="User">
        select * from smbms_user
    </select>

    <select id="getUserListByNullParam" resultType="User">
        select * from smbms_user
    </select>
    <!--  查询供应商记录数-->
    <select id="result" resultType="int">
        select count(1) as count from smbms_provider;
    </select>
    <!--  查询供应商列表 接口-->
    <select id="getProviderList" resultType="cn.kgc.pojo.Provider">
        select * from smbms_provider;
    </select>
    <!--   查询供应商列表 工具  -->
    <select id="providerlist" resultType="cn.kgc.pojo.Provider">
        select * from smbms_provider;
    </select>


</mapper>
Mapper
<?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="database.properties">
        <property name="driver" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms?serverTimezone=GMT%2B8&amp;zeroDateTimeBehavior=convertToNull"></property>
        <property name="username" value="root"></property>
        <property name="password" value="hxt990105"></property>
    </properties>
    <!--设置日记行为-->
    <settings>
        <setting name="logImpl" value="LOG4J"></setting>
        <setting name="lazyLoadingEnabled" value="false"></setting>
    </settings>
    <!--設置別名-->
    <typeAliases>
        <!--不推荐使用,当我们有很多的实体类的时候,这里配置的也很冗余
        <typeAlias type="cn.kgc.pojo.User"></typeAlias>-->
        <package name="cn.kgc.pojo"></package>
    </typeAliases>
    <!--配置运行环境-->
    <environments default="dev">
        <environment id="dev">
            <transactionManager type="JDBC"></transactionManager>
            <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>
        <environment id="test">
            <transactionManager type="JDBC"></transactionManager>
            <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>
        <environment id="pro">
            <transactionManager type=""></transactionManager>
            <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 resource="cn/kgc/dao/UserMapper.xml"></mapper>
    </mappers>
</configuration>
mybatis-config.xml
package cn.kgc.dao;

import cn.kgc.pojo.Provider;
import cn.kgc.pojo.User;

import java.util.List;

public interface UserMapper {
    public List<User> getUserListByNullParam();
    public List<Provider> getProviderList();
}
Mapper接口
package cn.kgc.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 MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;

    static {
        String path = "mybatis-config.xml";
        try {
            InputStream is = Resources.getResourceAsStream(path);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static SqlSession getSqlSession() {
        return sqlSessionFactory.openSession();
    }
}
mybatisUtils
package cn.kgc.test;

import cn.kgc.dao.UserMapper;
import cn.kgc.pojo.Provider;
import cn.kgc.pojo.User;
import cn.kgc.utils.MybatisUtils;
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 org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TestUserMapper {
    @Test
    public void test01(){
        //1.加载核心配置文件
        String path = "mybatis-config.xml";
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream(path);
            // 2.mybatis中的核心类
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            int result = sqlSession.selectOne("cn.kgc.dao.UserMapper.count");
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //使用工具類
    @Test
    public void test02(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        List<User> userList = sqlSession.selectList("cn.kgc.dao.UserMapper.userlist");
        for (User user : userList) {
            System.out.println("****************用戶名"+user.getUserName());
        }
    }
    //使用接口方式[以后就采用此种方式]
    @Test
    public void test03(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        List<User> userList = sqlSession.getMapper(UserMapper.class).getUserListByNullParam();
        for (User user : userList) {
            System.out.println("****************用戶名"+user.getUserName());
        }
    }
    @Test
    public void test04(){
        String path="mybatis-config.xml";
        InputStream is=null;
        try {
            is= Resources.getResourceAsStream(path);
            SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession=sqlSessionFactory.openSession();
            int result = sqlSession.selectOne("cn.kgc.dao.UserMapper.result");
            System.out.println("一共"+result+"条供应商记录。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test05(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        List<Provider> providerList = sqlSession.getMapper(UserMapper.class).getProviderList();
        for (Provider provider : providerList) {
            System.out.println("**供应商**"+provider.getProName()+"--"+provider.getProContact()+"--"+provider.getProPhone()+"--"+provider.getProDesc());
        }
    }
    @Test
    public void test06(){
        SqlSession sqlSession= MybatisUtils.getSqlSession();
        List<Provider> providerList=sqlSession.selectList("cn.kgc.dao.UserMapper.providerlist");
        for (Provider provider:providerList){
            System.out.println("**供应商**"+provider.getProName()+"--"+provider.getProContact()+"--"+provider.getProPhone()+"--"+provider.getProDesc());
        }
    }



}
test

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!