使用MyBatis Generator

1、添加依赖

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
    </dependencies>
</plugin>

2、添加config.xml

在resources目录下创建generatorConfig.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>

    <context id="DB2Tables" targetRuntime="MyBatis3">

        <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/demo"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <javaModelGenerator targetPackage="com.example.demo.model" targetProject="src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="mapper" targetProject="src\main\resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.mapper"
                             targetProject="src\main\java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <table tableName="country" domainObjectName="Country"></table>
    </context>
</generatorConfiguration>

<table>标签添加数据表和对应的实体类名

然后修改xml中的项目包名,jdbcConnection 

3、执行命令

mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate

此命令会覆盖原有Java文件(每次覆盖会在mapper.xml中double<resultmap>,<sql>,crud.注意删除)

4、application.propertity

mybatis.configuration.map-underscore-to-camel-case=true
mybatis.type-aliases-package=com.example.demo.model
mybatis.mapper-locations=classpath:mapper/*.xml

5、启动类添加

@MapperScan(basePackages = "com.example.demo.mapper")

如何添加联合查询

1、在SchoolMapper中添加接口

添加的接口为:

List<School> selectByExampleWithCountry(SchoolExample example);//用example查询出带国家信息的学校信息

School selectByPrimaryKeyWithCountry(Integer schoolId);//用primaryKey查询出带国家信息的学校信息

2、在school类中添加Country类型属性和构造方法

3、修改SchoolMapper.xml

<resultMap id="WithCountryResultMap" type="com.example.demo.model.School">
  <id column="school_id" jdbcType="INTEGER" property="schoolId" />
  <result column="name" jdbcType="VARCHAR" property="name" />
  <result column="country_id" jdbcType="INTEGER" property="countryId" />
  <result column="admin_id" jdbcType="INTEGER" property="adminId" />
  <!--指定联合查询出的国家字段的封装-->
  <association property="country" javaType="com.example.demo.model.Country">
    <id column="country_id" property="countryId"/>
    <result column="name" property="name"/>
  </association>
</resultMap>

4、添加<sql>和<select>

<sql id="WithCountry_Column_List">
    s.school_id, s.name, s.coutry_id, s.admin_id,c.country_id, c.name
  </sql>
<!--查询学校同时带国家信息-->
<select id="selectByExampleWithCountry" resultMap="WithCountryResultMap">
  select
  <if test="distinct">
    distinct
  </if>
  <include refid="WithCountry_Column_List" />
  from school s
  left join country c on s.country_id = c.country_id
  <if test="_parameter != null">
    <include refid="Example_Where_Clause" />
  </if>
  <if test="orderByClause != null">
    order by ${orderByClause}
  </if>
</select>

5、同理添加selectByExampleWithCountry

<!--查询学校并不带国家信息-->

<select id="selectByExample" parameterType="com.example.demo.model.SchoolExample" resultMap="BaseResultMap">
  select
  <if test="distinct">
    distinct
  </if>
  <include refid="Base_Column_List" />
  from school
  <if test="_parameter != null">
    <include refid="Example_Where_Clause" />
  </if>
  <if test="orderByClause != null">
    order by ${orderByClause}
  </if>
</select>

6、单元测试

在schoolmapper类中按CTRL+shift+T,创建测试类

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class SchoolMapperTest {
    /**
     * 测试SchoolMapper
     */
    @Autowired
    SchoolMapper schoolMapper;//报错无影响
    @Test
    public void testCRUD(){
        System.out.println(schoolMapper);
        //1、插入几个学校
        schoolMapper.insertSelective(new School(null,"test1",2,2));
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值