一. eclips安装mybaits插件
参考文章:http://www.cnblogs.com/zengsong-restService/archive/2013/08/09/3248245.html
概括:1. 下载压缩包,解压
2. 将解压出的jar包拷贝到 eclips安装目录下的plugin和feature 目录下,即可
二. 使用mybaits插件,自动连接DB,获取列名,生成对应的model 实体类、mapper xml文件,以及dao包(server接口类)
1.在使用mybaits generator 插件时,在eclips工程中,先添加 插件配置文件 generatorConfig.xml ,里面需要填写真实的DB 连接地址,所用到的table名。
修改文件内容:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > <generatorConfiguration > <context id="context1" > <jdbcConnection driverClass="com.informix.jdbc.IfxDriver" connectionURL="jdbc:informix-sqli://10.116.4.63:9088/lbpfadb:informixserver=lbpfa" userId="lbpfa" password="lbpfa" /> <javaModelGenerator targetPackage="pri.dbimpltest.sjy.model" targetProject="mybaitsAuto/src" /> <sqlMapGenerator targetPackage="pri.dbimpltest.sjy.sql" targetProject="mybaitsAuto/src" /> <javaClientGenerator targetPackage="pri.dbimpltest.sjy.mapper" targetProject="mybaitsAuto/src" type="XMLMAPPER" /> <table tableName="loanfiles" domainObjectName="LoanFile"> </table> <table tableName="loanbatchprocess" domainObjectName="TradeReqestInfo"> </table> </context> </generatorConfiguration>
配置好连接数据库及表的信息后就可以利用插件自动生成代码了。
三、利用步骤二 自动生成的代码类,构建mybaits实现。
1.首先 添加mybaits.xml 的配置文件,可以搜索 mybaits如何使用。
重点:在mapper标签里,指向第二部分自动生成的mapper.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 resource="database.properties"></properties> <environments default="development"> <environment id="development"> <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> </environments> <mappers> <mapper resource="pri/dbimpltest/sjy/sql/LoanFileMapper.xml"/> </mappers> </configuration>
比如这个:
mybaits.xml 里的 <properties resource="database.properties"></properties> 为的是,可手动修改db连接库文件,为以后多数据源留下便利。database.properties内容如下:
2. 在工程中使用说明,见附图 main方法。
import java.io.IOException; import java.io.InputStream; import java.util.List; 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 pri.dbimpltest.sjy.mapper.LoanFileMapper; import pri.dbimpltest.sjy.model.LoanFile; import pri.dbimpltest.sjy.model.LoanFileExample; public class mybaitsAutoTestMain { public static void main(String[] args) { // TODO Auto-generated method stub try { InputStream in = Resources.getResourceAsStream("mybatis.xml"); SqlSessionFactory sessionFacatory = new SqlSessionFactoryBuilder().build(in); SqlSession session =null; session = sessionFacatory.openSession(); LoanFileMapper mapper = session.getMapper(LoanFileMapper.class); System.out.println(" step 1---- insert-------!"); //插入需要 commit LoanFile arocd = new LoanFile(); arocd.setFilename("TEST--20170929"); arocd.setLoadtime("20202020"); arocd.setStatus("TEST"); mapper.insert(arocd); session.commit(); //重点! System.out.println(" step 2-----query------!"); //构造查询条件,可有可无,根绝实际情况 LoanFileExample aExample = new LoanFileExample(); aExample.createCriteria().andFilenameEqualTo("20170828WXFK.dat"); // aExample.createCriteria(); List<LoanFile> aLizi = mapper.selectByExample(aExample); for (LoanFile tmp :aLizi) { System.out.println(tmp); } System.out.println(" step 3-----------!"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("over!"); } }
有不明的请重点参考:
1 . http://www.cnblogs.com/zengsong-restService/archive/2013/08/09/3248245.html
2. http://blog.csdn.net/wyc_cs/article/details/9023117