带你搭建一个简单的mybatis项目:IDEA+spring+springMVC+mybatis+Mysql
最近小编有点闲,突发奇想想重温一下mybatis,然后在脑海中搜索了一下,纳尼,居然不太会用了,想到这里都是泪啊!!现在我所呆的的公司使用的是springboot+hebinate,编程都是使用的JPA语义化编程,都用不上mybatis了,时隔近8个月的时间没用它,它竟然想溜,然而毕竟它曾经来过,我怎么能轻易让它走,于是决定自己搭建一个,重温一下mybatis的使用,不过幸好的是,还真让我搭建成功了,废话不多说,上流程。
代码已经同步gitHub上:https://github.com/Slience-zae/mybatis-no-one.git
1、创建项目
在这里使用的是IDEA,首先就是搭建项目的老一套流程,file -> new -> project ->maven,一路next,按图填写完后直接next就好
2.添加依赖
找到pom文件,将下面的依赖加入到pom文件中,这个依赖就相当于我们在搭建普通项目时使用的jar包
<!-- 用来设置版本号 --> <properties> <srping.version>4.0.2.RELEASE</srping.version> <mybatis.version>3.2.8</mybatis.version> <slf4j.version>1.7.12</slf4j.version> <log4j.version>1.2.17</log4j.version> <druid.version>1.0.9</druid.version> </properties> <!-- 用到的jar包 --> <dependencies> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <!-- 表示开发的时候引入,发布的时候不会加载此包 --> <scope>test</scope> </dependency> <!-- spring框架包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${srping.version}</version> </dependency> <!-- mybatis框架包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> <!--mybatisPlus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.1</version> </dependency> <!-- 数据库驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- jstl标签类 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- log --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!--josn全家桶--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.2</version> </dependency> <!-- 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!--lombok,配合lombok使用,很好用功能很强大的插件--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.4</version> </dependency> <!-- 上传下载文件--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <!--servlet依赖包--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> </dependencies> <build> <!-- java编译插件,如果maven的设置里配置好jdk版本就不用 --> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!--mybatis自动逆向生成实体,mapper,xml的插件--> <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>
注意:这些依赖对于一个mybatis的基础项目不一定都需要,只是因为为了开发时方便,我把我认为的几个重要的也加上了,复制上这段依赖后,可能你代码会报红,这是因为你本地的maven仓库没有查到这些版本的依赖,它会自动下载,静静等候一段时间,等这些依赖包都不报红时,基本就可以了,怎么确认我们导入这些依赖成功了呢,我们可以在下面这个地方看一下。
3.添加配置文件
配置文件都统一放在resources目录下,关于配置文件的内容解释请看代码注释
配置文件一:jdbc.properties(数据库连接配置文件)
#数据库驱动 driver=com.mysql.jdbc.Driver #数据库连接地址(product是我的数据库名,这里换成你自己的) url=jdbc:mysql://localhost:3306/product?characterEncoding=utf8&useSSL=false #数据库的用户名 username=root #数据库的密码 password=root #定义初始连接数 initialSize=0 #定义最大连接数 maxActive=20 #定义最大空闲 maxIdle=20 #定义最小空闲 minIdle=1 #定义最长等待时间 maxWait=60000
配置文件二:log4j.properties(日志配置文件,可以不配置)
#日志输出级别 log4j.rootLogger=debug,stdout,D,E #设置stdout的日志输出控制台 log4j.appender.stdout=org.apache.log4j.ConsoleAppender #输出日志到控制台的方式,默认为System.out log4j.appender.stdout.Target = System.out #设置使用灵活布局 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout #灵活定义输出格式 log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} -[%p] method:[%c (%rms)] - %m%n
配置文件三:spring-mvc.xml(视图控制层配置文件)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 扫描注解,这样com.perfect包下的文件都能被扫描 ,换成你自己的包路径--> <context:component-scan base-package="com.perfect"/> <!-- 开启SpringMVC注解模式 --> <mvc:annotation-driven/> <!-- 静态资源默认servlet配置 --> <mvc:default-servlet-handler/> <!-- 配置返回视图的路径,以及识别后缀是jsp文件 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
配置文件四:applicationContext.xml(Spring的全局控制配置文件,最重要)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载properties文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"/> <!-- <property name="locations" value="classpath:log4j.properties"/> --> </bean> <!-- 配置数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </bean> <!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 扫描model包 --> <property name="typeAliasesPackage" value="com.perfect"/> <!-- 扫描sql配置文件:mapper需要的xml文件--> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!-- Mapper动态代理开发,扫描dao接口包--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 给出需要扫描Dao接口包 ,换成你自己的--> <property name="basePackage" value="com.perfect.moudle.system.dao"/> </bean> <!-- 事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--数据库连接池--> <property name="dataSource" ref="dataSource"/> </bean> <!--配置文件上传--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize"> <!-- 最大上传大小:5M --> <value>5242880</value> </property> </bean> </beans>
配置文件五:generatorConfig.xml(mybatis逆向生成代码配置文件,需要个人修改最多,按注释要求进行修改)
<?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"> <!-- 第二种mybatis逆向生成xml配置 --> <generatorConfiguration> <!-- 需要指明数据库连接器的绝对路径 --> <classPathEntry location="F:\learning\jar\mysql-connector-java-5.1.38.jar" /> <context id="sqlserverTables" targetRuntime="MyBatis3"> <!-- 生成的pojo,将implements Serializable--> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 数据库链接URL、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/product" userId="root" password="root"> </jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成model模型,对应的包路径,以及文件存放路径(targetProject),targetProject可以指定具体的路径,如./src/main/java, 也可以使用“MAVEN”来自动生成,这样生成的代码会在target/generatord-source目录下 --> <!--<javaModelGenerator targetPackage="com.joey.mybaties.test.pojo" targetProject="MAVEN">--> <javaModelGenerator targetPackage="com.perfect.moudle.system.entity" targetProject="./src/main/java"> <property name="enableSubPackages" value="true"/> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--对应的mapper.xml文件 --> <sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 对应的Mapper接口类文件 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.perfect.moudle.system.dao" targetProject="./src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 列出要生成代码的所有表,这里配置的是不生成Example文件 --> <table tableName="t_scf_risk_warn_detail" domainObjectName="RiskWarnDetail" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true" > <property name="useActualColumnNames" value="true"/> </table> </context> </generatorConfiguration>
注意:有些时候直接将配置文件复制上后,会出现第一行结尾的‘?’有红色提示的现象,多见于applicationContext.xml和spring_mvc.xml文件,这时候我们只需要把提示红的部分删去然后再重新加上,一般就可以解决了。我遇到这个现象时是这样解决的,如果解决不了那只好百度了,百度灭百分之90以上的bug。
4、调整项目结构
点击file --> Project Structure -->modules
① F:\old project\mybatis-no-one\src\main\webapp (注意:scr之前是你的项目路径,不用变,只需要复制scr以后的内容)
② F:\old project\mybatis-no-one\src\main\webapp\WEB-INF\web.xml(同上,只需要修改src之后的路径)
现在你的项目结构大体只这样的:
5.配置tomcat,启动项目
点击Run,选择弹出列表的第一个或者第二个,启动一下就好了,此时应该会跳到页面index.jsp,这是因为在spring_mvc中这样配置了,如果启动报错,可能有以下几种原因:
1.数据库配置错误,没有正确添加自己的数据库以及用户名和密码,导致连接失败。
2.applicationContext.xml配置有问题,扫描包的位置不对,注入失败错误应该此刻不会出现,因为还未写代码,还没哟创建controller,service,dao,bean等,但是后来可能启动会出现这个问题。
3.包冲突,依赖添加重复,删去多余依赖包即可(目前我搭建的这个项目没有存在这个问题)
6.添加数据库备用表,逆向生成实体,example以及mapper,mapper.xml等
在这里我简单提供几个数据库生成的sql,也可以不使用我的而使用你自己的,仅供参考
/* Navicat MySQL Data Transfer Source Server : xcrdb_dev Source Server Version : 50726 Source Host : 39.100.124.144:3306 Source Database : xcrdb_dev Target Server Type : MYSQL Target Server Version : 50726 File Encoding : 65001 Date: 2020-03-23 15:25:14 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for t_sys_user -- ---------------------------- DROP TABLE IF EXISTS `t_sys_user`; CREATE TABLE `t_sys_user` ( `id` varchar(255) COLLATE utf8_bin NOT NULL COMMENT '用户id', `name` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '姓名', `sex` int(11) DEFAULT NULL COMMENT '性别', `id_type` int(11) DEFAULT NULL COMMENT '身份证明类型', `id_no` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '身份证明id', `email` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '邮箱地址', `mobile` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '手机号码', `type` int(11) DEFAULT NULL COMMENT '类型', `status` int(11) DEFAULT NULL COMMENT '状态', `del_flag` int(11) DEFAULT NULL COMMENT '删除状态', `avatar` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '头像地址', `create_by` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '创建者ID', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `update_by` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '更新者ID', `update_time` datetime DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='用户表';
/* Navicat MySQL Data Transfer Source Server : mysql5.7 Source Server Version : 50717 Source Host : localhost:3306 Source Database : product Target Server Type : MYSQL Target Server Version : 50717 File Encoding : 65001 Date: 2020-03-31 16:01:08 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for t_scf_risk_warn_info -- ---------------------------- DROP TABLE IF EXISTS `t_scf_risk_warn_info`; CREATE TABLE `t_scf_risk_warn_info` ( `id` varchar(36) COLLATE utf8_bin NOT NULL COMMENT 'id', `warn_code` varchar(20) COLLATE utf8_bin DEFAULT NULL COMMENT '预警代码', `rule_type` char(1) COLLATE utf8_bin DEFAULT NULL COMMENT '规则分类', `rule_name` varchar(20) COLLATE utf8_bin DEFAULT NULL COMMENT '规则名称', `check_type` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '检验方式', `logical_symbol` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '逻辑符', `threshold` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '阈值', `warn_result` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '预警结果', `rule_condition` text COLLATE utf8_bin COMMENT '规则条件', `del_flag` int(11) DEFAULT NULL COMMENT '删除状态', `create_by` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '创建者ID', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `update_by` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '更新者ID', `update_time` datetime DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='预警设置'; -- ---------------------------- -- Records of t_scf_risk_warn_info -- ---------------------------- INSERT INTO `t_scf_risk_warn_info` VALUES ('252887583070097408', '1001', '0', '企业经营状态异常', '0', '', '', '0', 0x53454C45435420646F6D61696E5F636F646520646F6D61696E436F64652C6E616D652C756E6E6F20636F7270556E6E6F2046524F4D20745F7379735F6F72672057484552452064656C5F666C61673D3020414E44207374617475733D3120414E4420756E6E6F20494E202853454C45435420756E6E6F2046524F4D20745F63725F756E69745F696E666F2057484552452064656C5F666C61673D3020414E4420535542535452494E4728726F6C655F69642C332C32293D2730312720414E442028737461747573204E4F5420494E2028312C3229204F5220737461747573204953204E554C4C2929, '0', 'zcl_pt', '2020-03-19 10:17:19', 'zcl_pt', '2020-03-20 09:22:27'); INSERT INTO `t_scf_risk_warn_info` VALUES ('252889804868423680', '1002', '0', '机构状态异常', '0', ' ', ' ', '0', 0x53454C4543542069642C6E616D652C6372656469745F636F646520637265646974436F64652C756E6E6F20636F7270556E6E6F2C756E69745F73746174757320756E69745374617475732046524F4D20745F63725F756E69745F696E666F2057484552452064656C5F666C61673D3020414E442028756E69745F737461747573204E4F5420494E2028312C3229204F5220756E69745F737461747573204953204E554C4C29, '1', 'zcl_pt', '2020-03-19 10:26:09', 'zcl_pt', '2020-03-19 16:17:56'); INSERT INTO `t_scf_risk_warn_info` VALUES ('252890358726266880', '1003', '1', '项目授信状态失效', '0', ' ', ' ', '0', 0x73656C65637420612E636F72705F756E6E6F20636F7270556E6E6F2066726F6D20745F7363665F6974656D5F6C696D69745F6974656D5F696E666F2061206C656674206A6F696E20745F7379735F6F726720620A206F6E20612E636F72705F756E6E6F203D20622E756E6E6F20776865726520612E617574685F737461747573203D203320616E6420622E737461747573203D203120616E6420612E64656C5F666C6167203D3020616E6420622E64656C5F666C6167203D2030, '0', 'zcl_pt', '2020-03-19 10:28:21', 'mpb_pt', '2020-03-19 15:25:29'); INSERT INTO `t_scf_risk_warn_info` VALUES ('252890493082406912', '1004', '2', '项目状态失效', '0', ' ', ' ', '0', 0x73656C65637420612E636F72655F756E69745F756E6E6F20636F7270556E6E6F2066726F6D20745F7363665F6974656D5F646174612061206C656674206A6F696E20745F7379735F6F726720620A206F6E20612E636F72655F756E69745F756E6E6F203D20622E756E6E6F20776865726520612E737461747573203D203220616E6420622E737461747573203D203120616E6420612E64656C5F666C6167203D3020616E6420622E64656C5F666C6167203D2030, '0', 'zcl_pt', '2020-03-19 10:28:53', 'mpb_pt', '2020-03-19 15:27:21'); INSERT INTO `t_scf_risk_warn_info` VALUES ('252890667049553920', '1005', '2', '上下游企业关联状态失效', '0', ' ', ' ', '0', 0x73656C65637420612E636F72705F756E6E6F20636F7270556E6E6F2066726F6D20745F7363665F6974656D5F73636D2061206C656674206A6F696E20745F7379735F6F726720620A206F6E20612E636F72705F756E6E6F203D20622E756E6E6F20776865726520612E617574685F737461747573203D203320616E6420622E737461747573203D203120616E6420612E64656C5F666C6167203D3020616E6420622E64656C5F666C6167203D2030, '0', 'zcl_pt', '2020-03-19 10:29:35', 'mpb_pt', '2020-03-19 15:28:49'); INSERT INTO `t_scf_risk_warn_info` VALUES ('252890897203597312', '1006', '2', '保证金余额小于保证金比例*已用额度', '0', ' ', ' ', '0', 0x73656C65637420662E6974656D4E6F2C662E636F7270556E6E6F2066726F6D20280A2073656C656374202828706169645F6C696D6974202D2072657475726E65645F6C696D697429202A206465706F7369745F6D696E5F726174696F20292061732072654C696D6974202C20612E6974656D5F6E6F206173206974656D4E6F202C622E636F72655F756E69745F756E6E6F20617320636F7270556E6E6F202C202873656C65637420637572616D742066726F6D20745F7863725F61636E745F696E666F2077686572652061636E746E6F203D20636F6E63617428622E636F72655F756E69745F756E6E6F2C273036272C535542535452494E4728612E6974656D5F6E6F2C31342C34292C273032272920616E6420737461747573203D3120616E64202064656C5F666C6167203D2030202920617320636F6E202066726F6D20745F7363665F6974656D5F6C696D69745F6974656D5F696E666F2061200A4C454654204A4F494E20745F7363665F6974656D5F64617461206220206F6E20612E6974656D5F6E6F203D20622E6974656D5F6E6F0A6C656674206A6F696E20745F7379735F6F72672063206F6E20632E756E6E6F203D20622E636F72655F756E69745F756E6E6F0A77686572652020612E64656C5F666C6167203D203020616E6420622E64656C5F666C6167203D203020616E6420632E737461747573203D20310A2920662020776865726520662E636F6E203C2020662E72654C696D6974, '0', 'zcl_pt', '2020-03-19 10:30:29', 'lxf_pt', '2020-03-19 15:33:12'); INSERT INTO `t_scf_risk_warn_info` VALUES ('252891086945521664', '1007', '4', '企业>7天逾期笔数', '1', '大于', '5', '0', 0x73656C656374206D2E636F7270556E6E6F2066726F6D0A202873656C65637420636F72705F756E6E6F20415320636F7270556E6E6F2C636F756E7428696429206173206E756D2066726F6D20745F7363665F61737365745F6C6F616E5F64656269745F64657461696C2061207768657265204441544544494646284E4F5728292C412E706572696F645F646174655F726570617961626C6529203E203720616E6420612E64656C5F666C6167203D203020616E6420612E7061795F6261636B5F73746174757320696E20282732272C273327292067726F757020627920636F72705F756E6E6F290A2020206D0A2020206A6F696E20745F7379735F6F7267206E206F6E206D2E636F7270556E6E6F203D206E2E756E6E6F0A20202020207768657265206D2E6E756D3E3520616E64206E2E737461747573203D203120616E64206E2E64656C5F666C6167203D2030, '0', 'zcl_pt', '2020-03-19 10:31:15', 'zcl_pt', '2020-03-19 15:38:41'); INSERT INTO `t_scf_risk_warn_info` VALUES ('252891256865165312', '1008', '4', '企业>30天逾期笔数', '1', '大于', '10', '0', 0x73656C656374206D2E636F7270556E6E6F2066726F6D202873656C65637420636F72705F756E6E6F20415320636F7270556E6E6F2C636F756E7428696429206173206E756D2066726F6D20745F7363665F61737365745F6C6F616E5F64656269745F64657461696C2061207768657265204441544544494646284E4F5728292C412E706572696F645F646174655F726570617961626C6529203E20333020616E6420612E64656C5F666C6167203D203020616E6420612E7061795F6261636B5F73746174757320696E20282732272C273327292067726F757020627920636F72705F756E6E6F29206D0A202020206A6F696E20745F7379735F6F7267206E206F6E206D2E636F7270556E6E6F203D206E2E756E6E6F0A202020207768657265206D2E6E756D3E313020616E64206E2E737461747573203D203120616E64206E2E64656C5F666C6167203D2030, '0', 'zcl_pt', '2020-03-19 10:31:55', 'zcl_pt', '2020-03-19 15:35:21'); INSERT INTO `t_scf_risk_warn_info` VALUES ('252891452009353216', '1009', '4', '企业在平台最大逾期天数', '1', '大于', '30', '0', 0x73656C656374206D2E636F7270556E6E6F2066726F6D202873656C65637420612E636F72705F756E6E6F20415320636F7270556E6E6F2C6D617828612E6F7665726475655F6461797329206173206F766572647565446179732066726F6D20745F7363665F61737365745F6C6F616E5F64656269745F64657461696C206120776865726520612E64656C5F666C6167203D203020616E6420612E7061795F6261636B5F73746174757320696E20282732272C273327292067726F757020627920636F72705F756E6E6F29206D0A202020202020202020202020202020202020202020202020206A6F696E20745F7379735F6F7267206E206F6E206D2E636F7270556E6E6F203D206E2E756E6E6F0A7768657265206D2E6F766572647565446179733E333020616E64206E2E737461747573203D203120616E64206E2E64656C5F666C6167203D2030, '0', 'zcl_pt', '2020-03-19 10:32:42', 'zcl_pt', '2020-03-19 15:36:14'); INSERT INTO `t_scf_risk_warn_info` VALUES ('252891821187796992', '1010', '4', '企业在平台近一年超7天的逾期次数', '1', '大于', '10', '0', 0x73656C656374206D2E636F7270556E6E6F2066726F6D202873656C65637420636F72705F756E6E6F20415320636F7270556E6E6F2C636F756E7428696429206173206E756D2066726F6D20745F7363665F61737365745F6C6F616E5F64656269745F64657461696C206120776865726520612E6F7665726475655F64617973203E203720616E6420612E64656C5F666C6167203D203020616E64204441544544494646284355524441544528292C612E6372656174655F74696D6529203E203336352067726F757020627920636F72705F756E6E6F29206D0A202020202020202020202020202020202020202020202020206A6F696E20745F7379735F6F7267206E206F6E206D2E636F7270556E6E6F203D206E2E756E6E6F0A7768657265206D2E6E756D3E313020616E64206E2E737461747573203D203120616E64206E2E64656C5F666C6167203D2030, '0', 'zcl_pt', '2020-03-19 10:34:10', 'zcl_pt', '2020-03-19 15:37:25'); INSERT INTO `t_scf_risk_warn_info` VALUES ('252893151579082752', '1011', '5', '其他', '0', ' ', ' ', '0', 0x31, '1', 'zcl_pt', '2020-03-19 10:39:27', 'dew_pt', '2020-03-19 13:41:01'); INSERT INTO `t_scf_risk_warn_info` VALUES ('252981844763480064', '1012', '1', '123123123123123', '0', '', '', '0', 0x3132333132, '1', 'zhaoxulin_pt', '2020-03-19 16:31:53', 'zhaoxulin_pt', '2020-03-19 16:33:11'); INSERT INTO `t_scf_risk_warn_info` VALUES ('252984707636006912', '1013', '1', '0989098', '0', '', '', '1', 0x39373938393830, '1', 'zhaoxulin_pt', '2020-03-19 16:43:16', 'zhaoxulin_pt', '2020-03-19 16:44:09');
/* Navicat MySQL Data Transfer Source Server : mysql5.7 Source Server Version : 50717 Source Host : localhost:3306 Source Database : product Target Server Type : MYSQL Target Server Version : 50717 File Encoding : 65001 Date: 2020-03-31 16:01:24 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for t_scf_risk_flow_info -- ---------------------------- DROP TABLE IF EXISTS `t_scf_risk_flow_info`; CREATE TABLE `t_scf_risk_flow_info` ( `id` varchar(32) COLLATE utf8_bin NOT NULL COMMENT '主键', `prod_id` varchar(20) COLLATE utf8_bin DEFAULT NULL COMMENT '产品场景代码', `rule_setting` text COLLATE utf8_bin COMMENT '规则配置', `status` int(1) DEFAULT NULL COMMENT '状态', `del_flag` int(11) DEFAULT NULL COMMENT '删除状态', `create_by` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '创建者ID', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `update_by` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '更新者ID', `update_time` datetime DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='场景风控'; -- ---------------------------- -- Records of t_scf_risk_flow_info -- ---------------------------- INSERT INTO `t_scf_risk_flow_info` VALUES ('252893256814170112', 'logistics', 0x3235323839303335383732363236363838302C3235323839303439333038323430363931322C3235323839303636373034393535333932302C3235323839303839373230333539373331322C3235323839313038363934353532313636342C3235323839313235363836353136353331322C3235323839313435323030393335333231362C3235323839313832313138373739363939322C323532383837353833303730303937343038, '0', '0', 'zcl_pt', '2020-03-19 10:39:52', 'zcl_pt', '2020-03-20 09:31:36'); INSERT INTO `t_scf_risk_flow_info` VALUES ('252893367371829248', 'receivable', 0x323532383933313531353739303832373532, '1', '1', 'zcl_pt', '2020-03-19 10:40:18', 'dew_pt', '2020-03-19 13:23:40'); INSERT INTO `t_scf_risk_flow_info` VALUES ('252893493666516992', 'prepayments', 0x323532383933313531353739303832373532, '1', '1', 'zcl_pt', '2020-03-19 10:40:48', 'dew_pt', '2020-03-19 13:23:44'); INSERT INTO `t_scf_risk_flow_info` VALUES ('252893632271486976', 'stock', 0x323532383933313531353739303832373532, '1', '1', 'zcl_pt', '2020-03-19 10:41:21', 'dew_pt', '2020-03-19 13:23:47'); INSERT INTO `t_scf_risk_flow_info` VALUES ('252934605668093952', 'liqloan', 0x3235323838373538333037303039373430382C323532383839383034383638343233363830, '1', '1', 'dew_pt', '2020-03-19 13:24:10', 'zcl_pt', '2020-03-19 14:42:39'); INSERT INTO `t_scf_risk_flow_info` VALUES ('252959403739713536', 'liqloan', 0x323532383837353833303730303937343038, '0', '0', 'dew_pt', '2020-03-19 15:02:43', 'zcl_pt', '2020-03-19 16:18:30');
修改generatorConfig.xml中的内容,指定数据库表,生成位置等信息,点击运行,就能生成对应的代码文件了。
这个逆向生成只能一个表一个表的生成,多修改几次配置文件,最终的项目结构应该是这样的:
红框圈起来的便是逆向生成的代码了,注意啊,一个表只能逆向生成一次,连续点击两次会导致启动报错,如果想要重新生成就删除之前的文件再执行逆向生成操作。
接下来我们就愉快的写代码了!!!!
7、编码与测试
在这里我就打个样,不把全部的编程来一遍了,关于mybatis的使用,自行可以学习一下。
在实体的同级目录下,创建service,serviceImpl,controller三个包,然后再每个包下分别创建UserService,UserServiceImpl,UserController三个类,然后具体代码走起。
1 package com.perfect.moudle.system.service; 2 3 import com.perfect.moudle.system.entity.User; 4 5 import java.util.List; 6 7 public interface UserService { 8 /** 9 * 保存/更新用户 10 * @param user 11 */ 12 void saveUser(User user); 13 14 /** 15 * 删除用户 16 * @param id 17 * @return 18 */ 19 boolean deleteUser(String id); 20 21 /** 22 * 根据id查询用户的详情 23 * @param id 24 * @return 25 */ 26 User selectById(String id); 27 28 /** 29 * 获取用户列表 30 * @return 31 */ 32 List<User> selectUserList(); 33 34 /** 35 * 根据用户名模糊查询 36 * @param name 37 * @return 38 */ 39 List<User> selectUserLikeName(String name); 40 }
package com.perfect.moudle.system.serviceimpl; import com.perfect.common.utils.IdWorker; import com.perfect.moudle.system.dao.UserMapper; import com.perfect.moudle.system.entity.User; import com.perfect.moudle.system.entity.UserExample; import com.perfect.moudle.system.service.UserService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.UUID; @Service @Transactional public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public void saveUser(User user) { if(user.getId()!=null && !"".equals(user.getId())){//更新 userMapper.updateByPrimaryKey(user); }else{//保存 User newUser = new User(); //将对象赋值给新对象 BeanUtils.copyProperties(user, newUser,"id","create_time" ,"create_by","update_time","update_by","del_flag"); userMapper.insertSelective(newUser); } } @Override public boolean deleteUser(String id) { User user = userMapper.selectByPrimaryKey(id); user.setDel_flag(1); int count = userMapper.updateByPrimaryKey(user); if(count!=0){ return true; }else{ return false; } } @Override public User selectById(String id){ return userMapper.selectByPrimaryKey(id); } @Override public List<User> selectUserList() { return userMapper.selectByExample(null); } @Override public List<User> selectUserLikeName(String name) { UserExample userExample = new UserExample(); UserExample.Criteria criteria = userExample.createCriteria(); criteria.andNameLike(name); return userMapper.selectByExample(userExample); } }
package com.perfect.moudle.system.controller; import com.perfect.common.utils.ResultUtil; import com.perfect.common.vo.Result; import com.perfect.moudle.system.entity.User; import com.perfect.moudle.system.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping(value = "/user") public class UserController { @Autowired private UserService userService; /** * 保存/更新用户 * @param user * @return */ @RequestMapping(value = "/save",method = RequestMethod.POST) public Result saveUser(@RequestBody User user){ ResultUtil resultUtil = new ResultUtil(); userService.saveUser(user); return resultUtil.setSuccessMsg("保存成功"); } /** * 删除用户 * @param id * @return */ @RequestMapping(value = "/del",method = RequestMethod.GET) public Result delUser(@RequestParam(value = "id") String id){ boolean isOk = userService.deleteUser(id); if (isOk){ return new ResultUtil<>().setSuccessMsg("删除成功"); }else { return new ResultUtil<>().setErrorMsg("删除失败,请检查代码"); } } /** * 获取所有的用户列表 * @return */ @RequestMapping(value = "/findUserList",method = RequestMethod.GET) public Result findUserList(){ return new ResultUtil<>().setData(userService.selectUserList()); } /** * 根据id 获取用户信息 * @param id * @return */ @RequestMapping(value = "/findById",method = RequestMethod.GET) public Result findById(@RequestParam String id){ return new ResultUtil<>().setData(userService.selectById(id)); } }
接下来启动,用postman测试一下,一切都ok了。
若看完整代码,请访问gitHub:https://github.com/Slience-zae/mybatis-no-one.git
注:本篇博客皆为本人一字一句的敲打出来的,转载请备注出处,如有问题,敬请批评。