项目结构
所需jar包
创建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="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test" userId="root"
password="123456">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL
和 NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.xieli.liu.pojo"
targetProject="F:/JAVA23班第三组项目管理/JAVA23班第三组项目管理/newproject/newproject/src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.xieli.liu.mapper"
targetProject="F:/JAVA23班第三组项目管理/JAVA23班第三组项目管理/newproject/newproject/src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.xieli.liu.mapper" targetProject="F:/JAVA23班第三组项目管理/JAVA23班第三组项目管理/newproject/newproject/src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table tableName="user_info"></table>
</context>
</generatorConfiguration>
生成类
package com.xieli.liu;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2019-11-25.
*/
public class creact {
// 执行main方法以生成代码
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("F:/JAVA23班第三组项目管理/JAVA23班第三组项目管理/newproject/newproject/src/config/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
package com.xieli.liu.test;
import com.xieli.liu.mapper.UserInfoMapper;
import com.xieli.liu.pojo.UserInfo;
import com.xieli.liu.pojo.UserInfoExample;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* Created by Administrator on 2019-11-27
*/
public class test {
private ApplicationContext applicationContext;
UserInfoMapper userInfoMapper;
@Before
public void setup(){
applicationContext=new ClassPathXmlApplicationContext("classpath:config/application-config.xml");
userInfoMapper=(UserInfoMapper)applicationContext.getBean("userInfoMapper");
}
//根据主键查询
@Test
public void selectByPrimaryKey(){
UserInfo userInfo = userInfoMapper.selectByPrimaryKey(4);
System.out.println(userInfo.getUserName());
}
//自定义条件查询
@Test
public void selectByExample(){
UserInfoExample userInfoExample = new UserInfoExample();
//构建查询条件
UserInfoExample.Criteria criteria = userInfoExample.createCriteria();
//用户名=maomao
criteria.andUserNameEqualTo("maomao");
List<UserInfo> userInfo =userInfoMapper.selectByExample(userInfoExample);
System.out.println(userInfo.get(0).getNickName());
}
//添加数据
@Test
public void insert(){
UserInfo userInfo1 = new UserInfo();
userInfo1.setUserName("小兰");
userInfo1.setUserPwd("66666666666");
userInfoMapper.insert(userInfo1);
}
//添加数据
@Test
public void updateByPrimaryKey(){
//更新所有字段,需要先查询出来
//修改ID为4的用户名为小绿
/* UserInfo userInfo = userInfoMapper.selectByPrimaryKey(4);
userInfo.setUserName("小绿");
userInfoMapper.updateByPrimaryKey(userInfo);*/
//更新传入对象属性不为null的字段,适用批量更新,不需要先查询
//修改ID为41的用户名和密码
UserInfo userInfo1 = new UserInfo();
userInfo1.setUserId(41);
userInfo1.setUserName("小白");
userInfo1.setUserPwd("66666666666");
userInfoMapper.updateByPrimaryKeySelective(userInfo1);
}
}