关于mybatisConfig.xml 配置文件:
在Maven中的Pom中加入插件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
</plugin>
<?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>
<classPathEntry
location="D:\apache-maven-3.8.1\repository\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<property name="autoDelimitKeywords" value="false"/>
<property name="javaFileEncoding" value="UTF-8"/>
<property name="javaFormatt" />
<property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/hunau?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&allowMultiQueries=true&nullCatalogMeansCurrent=true"
userId="root" password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="com.bean" targetProject="src/main/java" >
<property name="constructorBased" value="true"/>
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<table tableName="emp" domainObjectName="EmpBean"
enableInsert="false"
enableSelectByPrimaryKey="false" ></table>
<table tableName="dept" domainObjectName="DeptBean"
enableInsert="false"
enableSelectByPrimaryKey="false" ></table>
</context>
</generatorConfiguration>
dtd约束 会爆红但是还能运行
ClassPathEntry中的location它代表着数据库驱动jar包位置,
targeRuntime中的值可以为Mybatis3和Mybatis3Simple 前者可以生成带条件的增删改查,
后者相对简单,只能生成基本的增删改查。
后面的property可以不用修改,
<javaModelGenerator targetPackage="com.bean" targetProject="src/main/java" >
<sqlMapGenerator targetPackage="com.mapper" targetProject="src/main/resources">
上面的代码表示自动生成的结果会在某某包下。
<table tableName="emp" domainObjectName="EmpBean">
上面代码表示从数据库中取得emp表,然后在bean目录下生成名字为EmpBean的实体类
最后就是Maven运行配置文件的一行代码:
mybatis-generator:generate -e
运行错误或者多运行了多次,必须把自动生成的文件删除再继续运行,
以下为targeRuntime中的值为Mybatis3运行的结果
各种带条件的查询方法:
import com.bean.EmpBean;
import com.bean.EmpBeanExample;
import com.mapper.EmpBeanMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.List;
public class Test001 {
@Test
public void testMybatis() throws IOException {
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession session =sessionFactory.openSession();
EmpBeanMapper mapper = session.getMapper(EmpBeanMapper.class);
EmpBeanExample empBeanExample= new EmpBeanExample();
EmpBeanExample.Criteria criteria = empBeanExample.createCriteria();
criteria.andDeptnoEqualTo((short)10);
criteria.andSalGreaterThan(new BigDecimal(1000));
List<EmpBean> empBeanList = mapper.selectByExample(empBeanExample);
for (EmpBean E:empBeanList){
System.out.println(E);
// Row: 7782, CLARK, MANAGER, 7839, 1981-06-09, 2450.00, null, 10
// Row: 7839, KING, PRESIDENT, null, 1981-11-17, 5000.00, null, 10
}
}
}
带条件的查询必须先new出Example实例,
Example中的createCriteria方法产生criteria内部类对象.
|