java中int类型强转char类型详解!!!!(超级详细!!真的!!!)

学习python!--的第二天--

  返回  

通过配置mybatis-config.xml,创建session会话,来整合spring和mybatis框架,并通过单元测试测试

2021/7/20 19:21:43 浏览:

新建一个maven工程,不使用接口的测试。

1.配置pom文件,加入jdbc和mybatis的jar包

<dependencies>
    <!--mybatis依赖包-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.4</version>
    </dependency>
    <!--jdbc依赖包-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.48</version>

    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>

    </dependency>
    </dependencies>

2.在resources中创建mybatis-config.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">

<!-- mybatis的核心配置文件 -->

<configuration>

    <typeAliases>
        <typeAlias type="cn.tedu.pojo.Dept" alias="Dept"></typeAlias>
    </typeAliases>
    <environments default="test">
        <!--        配置连接数据库的参数-->
        <environment id="test">
            <!--            使用事务管理器-->
            <transactionManager type="JDBC"></transactionManager>
            <!--            数据源,使用数据库连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?characterEncoding=utf8&amp;serverTimezone=Asia/Shanghai" />
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>

    <!--    引入映射文件-->
    <mappers>
        <mapper resource="DeptMapper.xml"></mapper>
    </mappers>
</configuration>

3.创建pojo实体类

package cn.tedu.pojo;
import lombok.Data;
@Data
public class Dept {
    private Integer id;
    private String dname;
    private String loc;
}

4.在resources中创建DeptMapper.xml文件,用来写sql语句。(记住:要将其引入到mybatis-config.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="DeptMapperNS">
    <!--    优化重复代码,使用sql标签和include标签-->
    <sql id="names">id,dname,loc</sql>
    <select id="get" resultType="cn.tedu.pojo.Dept">
        select <include refid="names"></include> from dept
    </select>

    <select id="getById" resultType="cn.tedu.pojo.Dept">
        select <include refid="names"></include> from dept
        <if test="id!=null">
            where id = #{id}
        </if>
    </select>
</mapper>

5.写测试类

1配置mybatis-config.xml核心文件

2创建一个会话工厂

3通过工厂创建会话对象

4通过会话对象执行sql语句(“sql文件的namespace.sql语句的唯一标识id”)

5处理结果

package cn.tedu;

import cn.tedu.pojo.Dept;
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.jupiter.api.Test;

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

public class Test1 {
    @Test
    public void get() throws IOException {
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = factory.openSession();
        List<Dept> list = session.selectList("DeptMapperNS.get");
        for(Dept  uu : list){
            System.out.println(uu);
        }
    }
    @Test
    public void get1() throws IOException {
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = factory.openSession();
        List<Dept> list = session.selectList("DeptMapperNS.getById", null);
        for(Dept u : list){
            System.out.println(u);
        }
    }
}

联系我们

如果您对我们的服务有兴趣,请及时和我们联系!

服务热线:18288888888
座机:18288888888
传真:
邮箱:888888@qq.com
地址:郑州市文化路红专路93号