码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis_1_搭建过程

时间:2020-09-17 22:20:19      阅读:26      评论:0      收藏:0      [点我收藏+]

标签:prope   depend   文件的   ase   get   管理器   apu   junit   nec   

1.添加依赖

<dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.0</version>
    </dependency>
    <dependency>
      <groupId>ognl</groupId>
      <artifactId>ognl</artifactId>
      <version>3.2.15</version>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.27.0-GA</version>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.30</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.30</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.13.3</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>3.3.0</version>
      <optional>true</optional>
    </dependency>

    <!-- Test dependencies -->
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.6.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.hsqldb</groupId>
      <artifactId>hsqldb</artifactId>
      <version>2.5.1</version>
      <scope>test</scope>
    </dependency>
    <dependency> <!-- 10.15+ need Java 9+ -->
      <groupId>org.apache.derby</groupId>
      <artifactId>derby</artifactId>
      <version>10.14.2.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <version>1.4.200</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>3.5.10</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-junit-jupiter</artifactId>
      <version>3.5.10</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.velocity</groupId>
      <artifactId>velocity-engine-core</artifactId>
      <version>2.2</version>
      <scope>test</scope>
    </dependency>
    <!-- postgresql driver is required to run the refcursor tests -->
    <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>42.2.16</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.21</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.assertj</groupId>
      <artifactId>assertj-core</artifactId>
      <version>3.17.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>eu.codearte.catch-exception</groupId>
      <artifactId>catch-exception</artifactId>
      <version>2.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.testcontainers</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>1.14.3</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.testcontainers</groupId>
      <artifactId>postgresql</artifactId>
      <version>1.14.3</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.testcontainers</groupId>
      <artifactId>mysql</artifactId>
      <version>1.14.3</version>
      <scope>test</scope>
    </dependency>

 

2.创建核心(全局)配置文件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">
<!-- 配置文件的根元素 -->
<configuration>
<!--属性:定义配置外在化 resource:读取项目路径下的属性文件;url:读取绝对路径下的属性文件-->
<properties resource="db.properties"></properties>
<!-- 设置:定义mybatis的一些全局性设置 -->
<settings>
<!-- 具体的参数名和参数值 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- 类型名称:为一些类定义别名 -->
<!--<typeAliases></typeAliases>-->
<!-- 类型处理器:定义Java类型与数据库中的数据类型之间的转换关系 -->
<!--<typeHandlers></typeHandlers>-->
<!-- 对象工厂 -->
<!--<objectFactory type=""></objectFactory>-->
<!-- 插件:mybatis的插件,插件可以修改mybatis的内部运行规则 -->
<!--<plugins>
<plugin interceptor=""></plugin>
</plugins>-->
<!-- 环境:配置mybatis的环境 default:默认环境的id -->
<environments default="development">
<!-- 环境变量:可以配置多个环境变量,比如使用多数据源时,就需要配置多个环境变量
id:数据库环境的唯一标识-->
<environment id="development">
<!-- 事务管理器 type="JDBC|MNAGED"-->
<transactionManager type="JDBC"></transactionManager>
<!-- 数据源 type="POOLED|UNPOOLED|JNDI"-->
<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>
<!-- 数据库厂商标识 -->
<!-- <databaseIdProvider type=""></databaseIdProvider>-->
<!-- 映射器:指定映射文件或者映射类 -->
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>

 

 

3.创建映射文件XxxMapper.xml,并配置,Xxx对应实体bean类

 

4.创建mapper接口,实现两个绑定

(1)接口全限定名和映射文件的namespace一致

(2)接口的方法名和SQL语句的id一致

<?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.mapper.UserMapper"><!--通过namespace赋值为XxxxMapper接口实现绑定 Id为指定方法-->
    <select id="getUserById" parameterType="Integer" resultType="com.pojo.User">
        SELECT id,username FROM t_user WHERE id =#{id}
    </select>
</mapper>

 

5.获取Mybatis操作数据库的会话对象

 

//读取配置文件
        InputStream resource = Resources.getResourceAsStream("mybatis-config.xml");
        //创建sqlsession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
        //获取sqlsession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //通过动态代理获取映射接口对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //执行方法
        User user = userMapper.getUserById(1);
        System.out.println(user);

 

6.测试

Mybatis_1_搭建过程

标签:prope   depend   文件的   ase   get   管理器   apu   junit   nec   

原文地址:https://www.cnblogs.com/A-PP-Z-droplet/p/13644320.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!