当前位置: 首页 > 后端技术 > Java

是什么让我节省了60%的编码时间?在SpringBoot中使用MyBatisGenerator

时间:2023-04-01 23:06:26 Java

MyBatis Generator绠€浠嬩笟鍔¢渶姹備笉鏂彉鏇达紝鏁版嵁搴撹〃缁撴瀯涓嶆柇淇敼锛屾槸鎴戜滑閫冧笉鍑虹殑瀹垮懡銆傚伐娆插杽鍏朵簨锛屽繀鍏堝埄鍏跺櫒锛屾槸鏃跺€欑キ鍑虹鍣ㄤ簡锛歁yBatis Generator锛堢畝绉帮細MBG锛夛紝瀹冩槸涓€涓敤浜庢墍鏈夌増鏈琈yBatis鐨勪唬鐮佽嚜鍔ㄧ敓鎴愬櫒銆傚畠鍙互鏍规嵁鏁版嵁搴撶殑琛ㄨ嚜鍔ㄤ负椤圭洰鐢熶骇瀵瑰簲鐨勫疄浣撶被銆丮apper銆丏AO锛屽寘鎷畝鍗旵RUD鏁版嵁搴撴搷浣?鍒涘缓銆佹煡璇€佹洿鏂般€佸垹闄?銆傝В鏀句簡鎴戜滑鐨勫弻鎵嬶紝涓嶅繀鍋氶噸澶嶆€х殑鏈烘宸ヤ綔銆傝妭鐪佷笅涓嶅皯鏃堕棿锛屼笉鐢ㄥ啀鑻﹀搱鍝堢殑鍔犵彮浜嗭紝杩樺彲浠ュ拰濡圭焊鍘荤害浼氥€傦紙鍓嶆彁鏄綘寰楀厛鏈変釜濡圭焊馃锛夊垱寤轰竴涓狹ySQL琛ㄤ负浜嗘柟渚挎紨绀哄垱寤轰竴涓狹ySQL琛紝琛ㄧ粨鏋勬瘮杈冪畝鍗曪紝鏄竴涓敤鎴蜂俊鎭〃锛欳REATE TABLE `user_info` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, `create_time` datetime DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;鍒涘缓涓€涓猄pringBoot椤圭洰浠ヤ娇鐢↖ntelliJ IDEA涓轰緥锛屽垱寤轰竴涓猄pringBoot椤圭洰銆傜偣鍑籉ile->New->Projects...锛岄€夋嫨Spring Initializr锛屽涓嬪浘锛氱偣鍑籒ext锛岃緭鍏roup銆丄rtifact绛変俊鎭紝濡備笅鍥撅細鐐瑰嚮Next锛岄€夋嫨Web锛屽苟鍕鹃€塖pring Web锛屽涓嬪浘锛氱偣鍑籒ext锛岃緭鍏roject name銆丳roject location绛変俊鎭紝濡備笅鍥撅細鏈€鍚庯紝鐐瑰嚮Finish锛屼竴涓猄pringBoot椤圭洰灏卞垱寤哄畬浜嗐€傚紩鍏yBatis Generator鐨凪aven鎻掍欢鍦╬om.xml鐨刾lugins鑺傜偣涓坊鍔犲涓嬪唴瀹癸細<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <configuration> <overwrite>true</overwrite> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> </dependencies></plugin>閰嶇疆MyBatis Generator鐨凪aven鎻掍欢鍦╮esources鏂囦欢澶逛腑鍒涘缓涓€涓猤eneratorConfig.xml鏂囦欢锛屽叾鍐呭濡備笅锛?lt;?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> <properties resource="application.properties"></properties> <!--defaultModelType鐢ㄤ簬鎸囧畾鐢熸垚瀵硅薄鐨勬牱寮忥紝flat琛ㄧず姣忎竴寮犺〃鍙敓鎴愪竴涓疄浣撶被锛岃繖涓疄浣撶被鍖呭惈琛ㄤ腑鐨勬墍鏈夊瓧娈点€?-> <context id="MySQLTables" targetRuntime="MyBatis3" defaultModelType="flat"> <property name="javaFileEncoding" value="UTF-8"/> <!-- 鐢熸垚鐨勫疄浣撶被瀹炵幇搴忓垪鍖栨帴鍙?--> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/> <commentGenerator> <property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"/> </commentGenerator> <!--鏁版嵁搴撹繛鎺ヤ俊鎭?-> <jdbcConnection driverClass="${spring.datasource.driverClassName}" connectionURL="${spring.datasource.url}" userId="${spring.datasource.username}" password="${spring.datasource.password}"> </jdbcConnection> <!-- 閰嶇疆鐢熸垚鐨勫疄浣撶被浣嶇疆 --> <javaModelGenerator targetPackage="one.more.mybatisgenerator.model" targetProject="src/main/java"> <!-- 璁剧疆鏄惁鍦╯etter鏂规硶涓紝瀵筍tring绫诲瀷瀛楁璋冪敤trim()鏂规硶 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 閰嶇疆鎺ュ彛浣嶇疆 --> <!-- type璁剧疆涓篈NNOTATEDMAPPER锛屽熀浜庢敞瑙g殑Mapper锛屼笉浼氭湁瀵瑰簲鐨剎ml鏂囦欢鐢熸垚--> <javaClientGenerator targetPackage="one.more.mybatisgenerator.mapper" targetProject="src/main/java" type="ANNOTATEDMAPPER"> </javaClientGenerator> <!-- 閰嶇疆鏁版嵁搴撹〃 --> <table tableName="user_info"> <!--鍦ㄧ敓鎴愮殑insert鍏冪礌涓婃坊鍔爑seGeneratedKeys=鈥漷rue鈥濆拰keyProperty灞炴€?-> <generatedKey column="id" sqlStatement="JDBC"/> </table> </context></generatorConfiguration>浠ヤ笂灏辨槸鏈€鍩虹绠€浠嬬殑閰嶇疆浜嗭紝鍦ㄥ疄闄呯殑寮€鍙戣繃绋嬩腑灏卞鐢ㄣ€傚鏋滄湁灏忎紮浼磋繕鏈夐渶瑕佹洿澶氱殑閰嶇疆鍔熻兘锛屽彲浠ュ畼鏂圭綉绔欙紙https://mybatis.org/generator/configreference/xmlconfig.html锛夋煡鐪嬨€傝嚜鍔ㄧ敓鎴愪唬鐮佷笅闈㈠氨鏄渶婵€鍔ㄤ汉蹇冪殑鏃跺埢浜嗭紝涓€閿嚜鍔ㄧ敓鎴愪唬鐮併€傚湪Maven鎻掍欢宸ュ叿鏍忎腑锛屽彲浠ョ湅鍒癿ybatis-generator鎻掍欢锛屽弻鍑诲叾涓殑generate閫夐」鍗冲彲锛屽涓嬪浘锛氭瀯寤烘垚鍔熶互鍚庯紝灏卞彲浠ョ湅鍒扮敓鎴愮殑浠g爜浜嗭紝濡備笅鍥撅細楠岃瘉鑷姩鐢熸垚鐨勪唬鐮侀獙璇佷箣鍓嶈繕鏈変竴涓楠や笉瑕佹紡鎺夛紝灏辨槸鍦ㄥ惎鍔ㄧ被涓婂姞涓奙apperScan娉ㄨВ锛屾瘮濡傦細@SpringBootApplication@MapperScan("one.more.mybatisgenerator.mapper")public class MybatisGeneratorDemoApplication { public static void main(String[] args) { SpringApplication.run(MybatisGeneratorDemoApplication.class, args); }}鏂板鏁版嵁闅忔満鐢熸垚涓€涓猆serInfo绀轰緥锛屾彃鍏ュ埌鏁版嵁搴撲腑锛? public UserInfo add() { Random random = new Random(System.currentTimeMillis()); UserInfo userInfo = new UserInfo(); userInfo.setName("name" + random.nextInt(100)); userInfo.setAge(random.nextInt(100)); userInfo.setCreateTime(new Date()); int rows = userInfoMapper.insert(userInfo); System.out.println("rows:" + rows); return userInfo; }鏌ヨ鏁版嵁鏌ヨ鏁版嵁搴撻噷age澶т簬鏌愪釜鍊肩殑user_info鏁版嵁锛? public List<UserInfo> getGreaterThan(Integer age) { UserInfoExample example = new UserInfoExample(); Criteria criteria = example.createCriteria(); criteria.andAgeGreaterThan(age); return userInfoMapper.selectByExample(example); }瀹屾暣鐨勭ず渚嬫簮鐮佸畬鏁寸殑绀轰緥婧愮爜鍙互鍘籬ttps://github.com/heihaozi/mybatis-generator-demo涓嬭浇銆?/p>