用mybatis原因很简单,易用,性能。是介于jdbc和hibernate之间的一个完美方案。

很简单:

1:配置pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.database</groupId>
<artifactId>dao-core</artifactId>
<version>0.0.1-SNAPSHOT</version> <dependencies> <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.1.1</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<type>jar</type>
<scope>test</scope>
</dependency> </dependencies> <build>
<plugins>
<!-- mybits dao层 自动生成代码 插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
</project>

2: src/main/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>
<!--classPathEntry:数据库的JDBC驱动 -->
<classPathEntry
location="D:\soft_源程序\DB\mysql\mysql-connector-java-5.1.19-bin.jar" /> <context id="MysqlTables" targetRuntime="MyBatis3"> <!-- 注意这里面的顺序确定的,不能随变更改 -->
<!-- 自定义的分页插件 <plugin type="com.deppon.foss.module.helloworld.shared.PaginationPlugin"/> --> <!-- 可选的(0 or 1) -->
<!-- 注释生成器 -->
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator> <!-- 必须的(1 required) -->
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/data?useUnicode=true&amp;characterEncoding=UTF-8"
userId="root" password="123654">
</jdbcConnection> <!-- 可选的(0 or 1) -->
<!-- 类型转换器或者加类型解析器 -->
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <!-- 必须的(1 required) -->
<!-- java模型生成器 -->
<!-- targetProject:自动生成代码的位置 -->
<javaModelGenerator targetPackage="com.test.model"
targetProject="F:\Workspaces\workspace_eclipse\dao-core\src\main\java">
<!-- TODO enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator> <!-- 必须的(1 required) -->
<!-- map xml 生成器 -->
<sqlMapGenerator targetPackage="com.test.persistence"
targetProject="F:\Workspaces\workspace_eclipse\dao-core\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator> <!-- 可选的(0 or 1) -->
<!-- mapper 或者就是dao接口生成器 -->
<javaClientGenerator targetPackage="com.test.dao"
targetProject="F:\Workspaces\workspace_eclipse\dao-core\src\main\java"
type="XMLMAPPER">
<property name="enableSubPackages" value="true" />
</javaClientGenerator> <!-- 必须的(1...N) -->
<!-- pojo 实体生成器 -->
<!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
<!-- schema即为数据库名 可不写 -->
<table schema="data" tableName="tab_city" domainObjectName="CityModel"
enableInsert="true">
<!-- 忽略字段 可选的(0 or 1) -->
<!-- <ignoreColumn column="is_use" /> -->
<!--//无论字段是什么类型,生成的类属性都是varchar。 可选的(0 or 1) 测试无效 -->
<!-- <columnOverride column="city_code" jdbcType="VARCHAR" /> -->
</table> </context>
</generatorConfiguration>
 

3: 点击pom,run as maven bulid  :  mybatis-generator:generate     红色字最好自己手敲或者copy.

构建完后:

4:项目中使用的话,需要建立 mybatis-config.xml 在 src/main/resources 下

<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/data" />
<property name="username" value="root" />
<property name="password" value="123654" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/test/persistence/CityModelMapper.xml" />
</mappers>
<!-- <typeAliases> <typeAlias type="com.hoo.entity.Account" alias="account"/>
</typeAliases> -->
</configuration>

5: 建立测试

5.1 java传统测试

TestDao.java

package com.test.dao;

import java.io.IOException;
import java.io.Reader; 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 com.test.model.CityModel; public class TestDao {
public static void main(String[] args) {
SqlSession session = null;
String resource = "mybatis-config.xml";
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
.build(reader);
session = sessionFactory.openSession(); CityModelMapper cityModelMapper = session
.getMapper(CityModelMapper.class); // CityModel cityModel = new CityModel();
// cityModel.setId(7);
// cityModel.setCityName("test7");
// cityModel.setCityCode(2);
// cityModelMapper.insert(cityModel);
// session.commit();
// System.out.println("插入一个city name" + cityModel.getCityName());
CityModel cityModel = cityModelMapper.selectByPrimaryKey(4);
session.commit();
System.out.println(cityModel.getCityName());
cityModel.setCityName("testchange");
;
cityModelMapper.updateByPrimaryKey(cityModel);
session.commit();
System.out.println("update"); } catch (IOException e) {
e.printStackTrace();
} finally {
if (null != session)
session.close();
}
}
}

5.2 单元测试:

CityModelMapperTest.java

package com.test.dao;

import static org.junit.Assert.fail;

import java.io.Reader;
import java.util.List; import junit.framework.Assert; 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.After;
import org.junit.Before;
import org.junit.Test; import com.test.model.CityModel;
import com.test.model.CityModelExample; public class CityModelMapperTest {
SqlSession session = null;
CityModelMapper cityModelMapper = null; @Before
public void setUp() throws Exception {
String resource = "mybatis-config.xml";
Reader reader = null;
reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
.build(reader);
session = sessionFactory.openSession();
cityModelMapper = session.getMapper(CityModelMapper.class);
} @Test
public void testCountByExample() {
fail("Not yet implemented");
} @Test
public void testDeleteByExample() {
fail("Not yet implemented");
} @Test
public void testDeleteByPrimaryKey() { int flag = cityModelMapper.deleteByPrimaryKey(10);
session.commit();
System.out.println("返回值:" + flag);
System.out.println("删除一个city id " + 10);
Assert.assertTrue(flag > 0);
} @Test
public void testInsert() {
CityModel cityModel = new CityModel();
int i = 10;
cityModel.setId(i);
cityModel.setCityName("test" + i);
cityModel.setCityCode(i);
cityModelMapper.insert(cityModel);
session.commit();
System.out.println("插入一个city name" + cityModel.getCityName());
} @Test
public void testInsertSelective() {
fail("Not yet implemented");
} @Test
public void testSelectByExample() {
CityModelExample example = new CityModelExample();
example.setDistinct(false);
example.setOrderByClause("id");
example.createCriteria().andIdBetween(1, 3);
List<CityModel> cityList = cityModelMapper.selectByExample(example);
for (CityModel cityModel : cityList) {
System.out.println("id:" + cityModel.getId() + "\n" + "name:"
+ cityModel.getCityName() + "\n");
}
fail("Not yet implemented");
} @Test
public void testSelectByPrimaryKey() {
CityModel cityModel = cityModelMapper.selectByPrimaryKey(8);
System.out.println("查询到一个city" + cityModel.getCityName());
Assert.assertTrue(null != cityModel);
} @Test
public void testUpdateByExampleSelective() {
fail("Not yet implemented");
} @Test
public void testUpdateByExample() {
fail("Not yet implemented");
} @Test
public void testUpdateByPrimaryKeySelective() {
fail("Not yet implemented");
} @Test
public void testUpdateByPrimaryKey() {
CityModel cityModel = new CityModel();
int i = 8;
cityModel.setId(i);
cityModel.setCityName("updaet" + i);
cityModel.setCityCode(i);
int flag = cityModelMapper.updateByPrimaryKey(cityModel);
session.commit();
System.out.println("返回值:" + flag);
System.out.println("update一个city id " + i);
Assert.assertTrue(flag > 0);
} @After
public void tearDown() throws Exception {
} }

6: 用到的sql

DROP TABLE IF EXISTS `tab_city`;
CREATE TABLE `tab_city` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`city_name` varchar(255) DEFAULT NULL COMMENT '城市名称',
`city_code` int(11) NOT NULL COMMENT '用三位数字表示 例如 001 代表朝阳',
`is_use` int(11) DEFAULT NULL COMMENT '是否有效',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='城市代码表';

完。

java_model_dao_自动生成_generator-mybatis-generator-1.3.2 基于maven插件的更多相关文章

  1. (二)一个很好用的自动生成工具——mybatis generator

    mybatis generator-自动生成代码 准备材料: 一个文件夹,一个数据库的驱动包,mybatis-generator-core-1.3.5.jar,一条生成语句 如图:(我用的是derby ...

  2. 【mybatis源码学习】利用maven插件自动生成mybatis代码

    [一]在要生成代码的项目模块的pom.xml文件中添加maven插件 <!--mybatis代码生成器--> <plugin> <groupId>org.mybat ...

  3. MyBatis Generator自动生成的配置及使用

    注意:文件名不能有中文字符,不然不能自动生成 找到MyBatis Generator.rar\MyBatis Generator\eclipse里的features和plugins文件,把这两个文件复 ...

  4. spring boot集成mybatis(3) - mybatis generator 配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  5. 【转】Intellij IDEA 14中使用MyBatis-generator 自动生成MyBatis代码

    Intellij IDEA 14 作为Java IDE 神器,接触后发现,非常好用,对它爱不释手,打算离开eclipse和myeclipse,投入Intellij IDEA的怀抱. 然而在使用的过程中 ...

  6. Spring Boot (七)MyBatis代码自动生成和辅助插件

    一.简介 1.1 MyBatis Generator介绍 MyBatis Generator 是MyBatis 官方出品的一款,用来自动生成MyBatis的 mapper.dao.entity 的框架 ...

  7. eclipse中mybatis自动生成插件使用

    对于使用Mybatis的开发者来说, 使用mybatis generator来生成mapper 以及配置文件, 可以大大简化工作, mybatis generator有多种工作方式, eclipse插 ...

  8. mybatis generator为实体类生成自定义注释(读取数据库字段的注释添加到实体类,不修改源码)

    我们都知道mybatis generator自动生成的注释没什么实际作用,而且还增加了代码量.如果能将注释从数据库中捞取到,不仅能很大程度上增加代码的可读性,而且减少了后期手动加注释的工作量. 1.首 ...

  9. 通过eclipse mybatis generater代码生成插件自动生成代码

    Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件.通过在Ecl ...

随机推荐

  1. 在线API大全

    之前整理过几个经常使用api地址,在经常使用在线API集合博文中. 近期浏览网页的时候,又发现一个很完整的api的大全,分享出来,建议大家收藏起来,用的时候方便查询. 经常使用API文档索引http: ...

  2. 解决Fedora升级时nvidia显卡问题

    ​ 升级到新版Fedora后登录不了gnome 小编最近升级了Fedora 20到21,结果就如之前从Fedora 19升级到20时类似,又出问题了.Fedora你到底行不行... gnome登录不了 ...

  3. Swing动画之游戏角色

    一.动画效果:实现了飞机飞行的动画效果,也实现了飞机的移动. 二.实现原理: 1.飞机飞行 的效果:事实上也还是重写paintComponent,依照一定的时间间隔更换图片就有了飞行的效果,动画就是更 ...

  4. PHP于DIRECTORY_SEPARATOR任务

    DIRECTORY_SEPARATOR是php的内部常量.用于显示系统分隔符的命令,不须要不论什么定义与包括就可以直接使用. 在windows下路径分隔符是\(当然/在部分系统上也是能够正常执行的), ...

  5. 由iPhone emoji问题牵出UTF-16编码,UTF-8编码查询

    前言 iOS平台,系统输入法emoji表达.表达式不能在很多其他平台上显示,尤其是在Android.Symbian系统.我决定到底要探索1:我指的是一些知识: (注意:该博文已经如果读者已经了解utf ...

  6. 关于Windows azure从github上部署项目

    自己做了一个闪存解析的webapi,今天尝试了一下加一个HelpPage,本地访问正常,但是在azure上就报错. 项目是不熟在WindowsAzure上的,项目自动同步github上的项目.gith ...

  7. Hadoop加zookeeper构建高可靠集群

    事前准备 1.更改Linux主机名,每个人都有配置 vim /etc/sysconfig/network NETWORKING=yes HOSTNAME=hadoop-server1 2.改动IP / ...

  8. POJ 1252 Euro Efficiency

    背包 要么 BFS 意大利是说给你几个基本的货币,组成 1~100 所有货币,使用基本上的货币量以最小的. 出口 用法概率.和最大使用量. 能够BFS 有可能 . 只是记得数组开大点. 可能会出现 1 ...

  9. 软测试综述——PV操作

     在操作系统中,进程之间常常会存在相互排斥(都须要共享独占性资源时)和同步(完毕异步的两个进程的协作)两种关系.而信号量和PV操作完美有效的处理了这两种情况.     相互排斥:就好比过独木桥,一 ...

  10. NSIS:强制结束软件进程

    原文NSIS:强制结束软件进程 有时候,我们选择卸载软件后发现安装目录中的主文件依然存在,不是我们卸载代码写的不对,而是卸载的时候软件根本就没有关闭! 在卸载前加上下面这个宏可以在一定程度上免除上述的 ...