好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

使用MyBatis进行数据库映射的方式

在java中,mybatis是1个支持自定义sql的持久层框架,它可以存储过程和高级的 映射 。

在正式介绍之前,我们首先通过mvn进行安装,我们将下面的内容添加到 pom.xml 配置文件中:

?

1

2

3

4

5

<dependency>

  <groupid>org.mybatis</groupid>

  <artifactid>mybatis</artifactid>

  <version> 3.4 . 5 </version>

</dependency>

上述操作完成后,我们首先新建1个mybatis的配置文件,使用xml的格式进行编写:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<?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" ></properties>

  <environments default = "my-config" >

    <environment id= "my-config" >

     <transactionmanager type= "jdbc" />

     <datasource type= "pooled" >

      <property name= "driver" value= "${driver}" ></property>

      <property name= "url" value= "${url}" ></property>

      <property name= "username" value= "${user}" ></property>

      <property name= "password" value= "${pwd}" ></property>

     </datasource>

    </environment>

  </environments>

  <mappers>

   <mapper resource= "mapping.xml" />

  </mappers>

</configuration>

我们将上述的内容写入到 config.xml 文件中。其中environments是配置的环境,其中的environment用于对应的环境的配置。这里,我们默认使用的环境是 my-config ,其使用jdbc的方式进行连接,数据源的方式为连接池。其中,数据源可以使用:

unpooled,每次请求时打开和关闭连接 pooled,连接池 jndi,主要用于ejb或应用服务器中使用

而环境通过对 db.properties 中的内容进行配置,其内容如下:

?

1

2

3

4

url=jdbc:oracle:thin: @xxx .xxx.xxx.xx: 1521 /orcl

user=xxx

pwd=xxx

driver=oracle.jdbc.driver.oracledriver

更多关于mybatis的配置,可以  参考   。其中包括别名、类型处理器typehandler的说明,这里就不一一叙述了。

最后,我们还设置了1个匹配的资源,其支持4种方式:

resource,使用相对于类路径的资源引用

url,使用完全限定资源定位符

class,使用映射器接口实现类的完全限定类名

name,将包内的映射器接口实现全部注册为映射器

在这里我们使用resource的方式指定匹配的文件的名称。其内容如下:

?

1

2

3

4

5

6

7

8

9

<?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= "user" >

   <select id= "es_temp" fetchsize= "10" resulttype= "java.util.linkedhashmap" >

    select entname,uniscid,industry,region,opstate,estyear,scale from xxx where rownum <![cdata[<=]]> 20

   </select>

</mapper>

由于在xml中,括号具有单独的意义,因此如果我们使用尖括号会出现解析错误,此时我们可以使用html实体转义编码,如 < 来进行处理,或者直接使用 <![cdata[xxxx]]> 进行实际符号的处理。

我们通过mapper标签中的namespace定义了映射的命名空间,其中的id用于对应语句的匹配,方便进行命名解析。

接着是对应的java代码的编写:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

import java.io.reader;

import java.util.*;

import com.alibaba.fastjson.*;

import org.apache.ibatis.io.resources;

import org.apache.ibatis.session.*;

public class demo {

   public static sqlsessionfactory sessionfactory;

   public static void main(string[] args) {

     try {

       reader reader = resources.getresourceasreader( "config.xml" );

       sessionfactory = new sqlsessionfactorybuilder().build(reader);

       sqlsession session = sessionfactory.opensession();

       list<linkedhashmap<?,?>> result = session.selectlist( "user.es_temp" );

       string json_str = json.tojsonstring(result);

       system.out.println(json_str);

       session.close();

     } catch (exception e){

       e.printstacktrace();

     }

   }

}

在这里,我们通过resources类对配置文件进行读取,然后构建1个sqlsessionfactory,之后通过其opensession得到1个session对象,这样我们就可以进行对应的sql操作了。

我们使用selectlist函数进行对应sql的执行,其使用的是 命名空间.id 的方式来进行对应的操作。另外,还有1个selectone函数用于单个记录的查询,对于多个结果会直接返回异常。

其结果为:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

[

   {

     "entname" : "深圳市蓝海汇装饰设计工程有限公司" ,

     "uniscid" : "91440300359236108q" ,

     "industry" : "批发和零售业" ,

     "region" : "深圳" ,

     "opstate" : "存续(在营、开业、在册)" ,

     "estyear" : "3年以下" ,

     "scale" : "小微企业"

   },

   {

     "entname" : "暴风雨(深圳)电子商务有限公司" ,

     "uniscid" : "91440300ma5df887xe" ,

     "industry" : "批发和零售业" ,

     "region" : "深圳" ,

     "opstate" : "存续(在营、开业、在册)" ,

     "estyear" : "3年以下" ,

     "scale" : "小微企业"

   },

   ...

]

可以看到,通过上述的方式,我们可以很容易的实现代码与sql语句的分离。但是,对应的sql语句依赖于 数据库 ,可移植性差。另外,对于字段多、关联表多时,编写sql语句的工作量很大。

总结

以上所述是小编给大家介绍的使用mybatis进行数据库映射,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

原文链接:http://blog.52sox测试数据/Java-use-Mybatis-reflect-database/

查看更多关于使用MyBatis进行数据库映射的方式的详细内容...

  阅读:15次