#Mybatis项目(idea版本,eclipse版本自行脑补)
##1、Mybatis逆向工程
1、创建普通的maven工程
<groupId>com.shuaijx</groupId>
<artifactId>mybatisGenerator</artifactId>
<version>1.0-SNAPSHOT</version>
2、导入generatorConfig.xml
一般该配置文件放在resources/generator 目录下面
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">
<!--
使用本配置最重要的几个地方,
1、修改mysql-connector-jar的本地路径
2、修改连接,用户名,密码
3、修改数据库表及对应的java类
-->
<generatorConfiguration>
<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
<classPathEntry location="E:\tools\mysql-connector-java-5.1.44\mysql-connector-java-5.1.44-bin.jar"/>
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.56.102:3306/pinyougoudb" userId="root" password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.pinyougou.pojo" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<!-- 在getter方法中,对String类型字段调用trim()方法 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="com.pinyougou.mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.pinyougou.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="tb_address" domainObjectName="Address"></table>
<table tableName="tb_areas" domainObjectName="Areas"></table>
<table tableName="tb_brand" domainObjectName="Brand"></table>
<table tableName="tb_cities" domainObjectName="Cities"></table>
<table tableName="tb_content" domainObjectName="Content"></table>
<table tableName="tb_content_category" domainObjectName="ContentCategory"></table>
<table tableName="tb_freight_template" domainObjectName="FreightTemplate"></table>
<table tableName="tb_goods" domainObjectName="Goods"></table>
<table tableName="tb_goods_desc" domainObjectName="GoodsDesc"></table>
<table tableName="tb_item" domainObjectName="Item"></table>
<table tableName="tb_item_cat" domainObjectName="ItemCat"></table>
<table tableName="tb_item_spec_option" domainObjectName="ItemSpecOption"></table>
<table tableName="tb_order" domainObjectName="Order"></table>
<table tableName="tb_order_item" domainObjectName="OrderItem"></table>
<table tableName="tb_pay_log" domainObjectName="PayLog"></table>
<table tableName="tb_provinces" domainObjectName="Provinces"></table>
<table tableName="tb_seckill_goods" domainObjectName="SeckillGoods"></table>
<table tableName="tb_seckill_order" domainObjectName="SeckillOrder"></table>
<table tableName="tb_seller" domainObjectName="Seller"></table>
<table tableName="tb_specification" domainObjectName="Specification"></table>
<table tableName="tb_specification_option" domainObjectName="SpecificationOption"></table>
<table tableName="tb_type_template" domainObjectName="TypeTemplate"></table>
<table tableName="tb_user" domainObjectName="User"></table>
</context>
</generatorConfiguration>
3、需要修改文件里面的几个注意点:
1、修改mysql-connector-jar的本地路径
2、修改连接,用户名,密码
3、修改数据库表及对应的java类
4、添加maven逆向工程生成代码的依赖
在pom里面添加
<build>
<plugins>
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
5、添加maven指令
mybatis-generator:generate -e
接下来就是运行
##2、开始进行逆向工程测试
6、 导入Mybatis运行及测试的最小依赖,开始进行项目测试
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
7、开始创建配置文件
1. SqlMapConfig.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="db.properties">
<!--properties中还可以配置一些属性名和属性值 -->
<!-- <property name="jdbc.driver" value=""/> -->
</properties>
<!-- 全局配置参数,需要时再设置 -->
<!-- <settings>
</settings> -->
<!-- 别名定义 -->
<typeAliases>
<!-- 针对单个别名定义
type:类型的路径
alias:别名
-->
<!-- <typeAlias type="cn.itcast.mybatis.po.User" alias="user"/> -->
<!-- 批量别名定义
指定包名,mybatis自动扫描包中的po类,自动定义别名,别名就是类名(首字母大写或小写都可以)
-->
<package name="com.pinyougou.pojo"/>
</typeAliases>
<!-- 和spring整合后 environments配置将废除-->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理,事务控制由mybatis-->
<transactionManager type="JDBC"/>
<!-- 数据库连接池,由mybatis管理-->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--大配置关联小配置-->
<mappers>
<!--关联单个小配置,不能重复关联-->
<!--<mapper resource="cn/happy/dao/IUserDAO.xml"/>-->
<!--关联整个包,dao层接口名字和对应的xml名字要一样-->
<package name="com.pinyougou.mapper"/>
</mappers>
</configuration>
2. db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.56.102:3306/pinyougoudb
jdbc.username=root
jdbc.password=root
3. log4j.properties
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
8、 创建mybatis数据源获取utils
创建utils文件夹,将MybatisUtil创建出来
MyBatisUtil
package com.pinyougou.utils;
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 java.io.IOException;
import java.io.InputStream;
/**
* @prrgrame: mybatisGenerator
* @description: Mybatis获取配置文件创建数据源session工具类
* @author: shuaijiaxing
* @create: 2018-09-08 23:24
*/
public class MyBatisUtil {
private static InputStream is;
private static SqlSessionFactory sqlSessionFactory;
static{
try {
is = Resources.getResourceAsStream("SqlMapConfig.xml");
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
}
private MyBatisUtil() {
}
public static SqlSession getSession(){
return sqlSessionFactory.openSession();
}
}
9、 编写测试类
创建service类
UserService
package com.pinyougou.service;
import com.pinyougou.mapper.BrandMapper;
import com.pinyougou.mapper.UserMapper;
import com.pinyougou.pojo.Brand;
import com.pinyougou.pojo.User;
import com.pinyougou.pojo.UserExample;
import com.pinyougou.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @prrgrame: mybatisGenerator
* @description:
* @author: shuaijiaxing
* @create: 2018-09-08 23:23
*/
public class UserService {
@Test
public void test1(){
SqlSession session = MyBatisUtil.getSession();
BrandMapper mapper = session.getMapper(BrandMapper.class);
List<Brand> brands = mapper.selectByExample(null);
for (Brand brand : brands) {
System.out.println(brand.toString());
}
}
@Test
public void test2(){
SqlSession session = MyBatisUtil.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
UserExample userExample = new UserExample();
List<String> names = new ArrayList<String>();
names.add("nezha");
names.add("taiba");
userExample.createCriteria().andUsernameIn(names);
List<User> users = mapper.selectByExample(userExample);
for (User user : users) {
System.out.println(user.toString());
}
}
}