码上敲享录 > mybatis的常见问题 > java中使用mybaitis,不依赖任何架构

java中使用mybaitis,不依赖任何架构

章节目录下一章 2019-08-09已有1819人阅读 评论(0)

java中使用mybaitis,不依赖任何架构


解决方法:

1.maven依赖:

<dependency>

           <groupId>org.mybatis</groupId>

           <artifactId>mybatis</artifactId>

           <version>3.5.1</version>

       </dependency>


2.核心代码

import com.juchn.call.center.dao.entity.DialplanDatailsEntity;

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.InputStream;


class DiaplanDatailsTest {

    public static void main(String[] args) throws Exception {

       String resource = "mybatis\\mybatis-config.xml";

       InputStream inputStream = Resources.getResourceAsStream(resource);

       SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


       SqlSession session = sqlSessionFactory.openSession();

       DiaplanDetailsMapper mapper = session.getMapper(DiaplanDetailsMapper.class);

       DialplanDatailsEntity entity =mapper.select("04c9a55c-ceb0-4d2f-802d-675a4d019229");//DialplanDatailsEntity是你自定义的实体


   }

}


3.DialplanDatailsEntity 实体类

public class DialplanDatailsEntity {

   private String dialplanDetailUuid;

   private String domainUuid;

   private String dialplanUuid;

  //省略set  get

}


4.src\main\resources\mybatis\DiaplanDatailsMapper.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.juchn.call.center.dao.mapper.DiaplanDetailsMapper">


   <resultMap type="com.juchn.call.center.dao.entity.DialplanDatailsEntity" id="DialplanDatails">

       <id column="dialplan_detail_uuid" property="dialplanDetailUuid"/>

       <result column="domain_uuid" property="domainUuid"/>

       <result column="dialplan_uuid" property="dialplanUuid"/>

   </resultMap>


   <select id="select" resultMap="DialplanDatails">

       select *

       from v_dialplan_details

       where dialplan_detail_uuid = #{id}

   </select>


</mapper>


5.src\main\resources\mybatis\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>


   <properties>

       <property name="driver" value="com.mysql.jdbc.Driver"/>


       <!--本地-->

       <property name="url"

                 value="jdbc:mysql://192.168.0.167:3306/frees?verifyServerCertificate=false&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8"/>

       <property name="username" value="wlg"/>

       <property name="password" value="123456"/>


     


   </properties>


   <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 resource="mybatis/DiaplanDatailsMapper.xml"/>

   </mappers>

</configuration>


0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交