mybatis逆向工程mbg
阿新 • • 發佈:2018-02-23
cto mbg value 查詢 哪些 table resource user mage
mbg:mybatis generator=mybatis代碼生成器
1.看一下項目結構
其中bean文件,mapper接口文件和mapper.xml文件是代碼生成器自動生成的。
使用generator生成器的方法,百度mybatis,進入git倉庫,點mybatis,找到generator就是生成器的源碼和使用方法說明書。
首先,編寫一個dmscq.xml配置文件。
<?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> <!-- targetRuntime="MyBatis3":生成mybatis豪華框架,如果是MyBatis3Simple則生成簡單框架 --> <context id="DB2Tables" targetRuntime="Mybatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/db_blog?allowMultiQueries=true"userId="root" password="5678"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 指定javabean的生成策略 targetPackage:目標包名 targetProject:目標工程--> <javaModelGeneratortargetPackage="com.atguigu.mybatis.bean" targetProject=".\src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- sqlMapGenerator:sql映射生成策略: --> <sqlMapGenerator targetPackage="com.atguigu.mybatis.dao" targetProject=".\conf"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- javaClientGenerator:指定mapper接口所在的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mybatis.dao" targetProject=".\src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 指定要逆向分析哪些表:根據表要創建javaBean --> <table tableName="t_blogger" domainObjectName="Blogger"></table> <table tableName="t_dep" domainObjectName="Department"></table> </context> </generatorConfiguration>
寫好配置文件後,去test類中寫一個生成的方法。
package com.atguigu.mybatis.test; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; 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 org.junit.Test; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import com.atguigu.mybatis.bean.Blogger; import com.atguigu.mybatis.bean.BloggerExample; import com.atguigu.mybatis.bean.BloggerExample.Criteria; import com.atguigu.mybatis.dao.BloggerMapper; public class MyBatisTest { public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(inputStream); }
//生成代碼根據配置文件 @Test public void runDmscq() throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("dmscq.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); }
//測試生成代碼 @Test public void getBloggerByExample() throws Exception { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); BloggerMapper mapper = openSession.getMapper(BloggerMapper.class); //selectByExample:查詢根據條件,條件為null則是查詢所有。 /*List<Blogger> selectByExample = mapper.selectByExample(null); for (Blogger blogger : selectByExample) { System.out.println(blogger); }*/ //查詢username中帶e的並且profile中帶e的 BloggerExample bloggerExample = new BloggerExample(); Criteria createCriteria = bloggerExample.createCriteria(); createCriteria.andUsernameLike("%e%"); createCriteria.andProfileLike("%e%"); /*List<Blogger> list = mapper.selectByExample(bloggerExample); for (Blogger blogger : list) { System.out.println(blogger); }*/ //如果加個條件,查詢username中帶e的並且profile中帶e的,(或者sign中帶c的) Criteria criteriaCriteria2 = bloggerExample.createCriteria(); criteriaCriteria2.andSignLike("%c%"); //和之前的條件進行拼裝 bloggerExample.or(criteriaCriteria2); List<Blogger> blo = mapper.selectByExample(bloggerExample); for (Blogger blogger : blo) { System.out.println(blogger); } } }
以上測試代碼分別測試了,黃色查詢所有的blogger,綠色的查詢username和profile中都帶e的。最後所有沒註釋掉的代碼是查詢username和profile中都帶e或者sign中帶c的
mybatis逆向工程mbg