1、mybatis-generator 生成DAO、Mapper、entity

所需环境:jdk

所需jar包:mybatis-generator-core-1.3.5.jar、MySQL-connector-java-5.1.34.jar

:新建一个文件夹generator(这里我是在E盘新建的E:\generator)用来存放工具jar包和配置文件

:将准备好的两个jar包直接放在generator文件夹下面

:在新建的文件夹下面新建一个文件,命名:generator.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>
<!-- 数据库驱动包位置 -->
<classPathEntry location="E:\generator\mysql-connector-java-5.1.34.jar" />
<!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/bruce_test?characterEncoding=utf8" userId="root" password="123456">
</jdbcConnection> <javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="andy.model" targetProject="E:\generator\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成的映射文件包名和位置 -->
<sqlMapGenerator targetPackage="andy.mapping" targetProject="E:\generator\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="andy.dao" targetProject="E:\generator\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 多个表直接复制下面的,更改tableName和domainObjectName就可以-->
<table tableName="bruce_userinfor" domainObjectName="Userinfor" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>

四 :打开cmd命令提示符,进入到E:\generator,输入命令:java -jar mybatis-generator-core-1.3.5.jar -configfile generator.xml -overwrite

2、mapper中传入多个参数

DAO层(不传入的参数不要使用@Param注解):

package com.login.dao;

import org.apache.ibatis.annotations.Param;

import com.login.entity.User;

public interface IUserMapper {

  User getUserByUsername(@Param("username") String username,
@Param("password") String password);
}

mapper层:

<?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="com.login.dao.IUserMapper"> <select id="getUserByUsername" parameterType="map" resultType="com.login.entity.User">
SELECT * FROM user WHERE username = #{username} and password= #{password}
</select>
</mapper>
05-28 21:45