MyBatisCodeHelper-Pro的介绍和使用 这次要介绍的是一款名为mybatiscodehelper的插件,这款插件非常强大,而且灵活,主要的功能是能自动帮我们实现一些基本的业务代码。在我们使用这个idea编写crud代码的时候,能发挥出巨大的作用。
阅读本文需要点基础。你先对mybatis有一些基本的认知,或者熟练使用mybatis等功能之后。在开始使用这个插件。
基本使用 我们首先打开idea。从右边找到数据源这个选项。先用这个数据源测试一下和MySQL的连接。这个连接还需要下载一些jar包。
等连接成功后,我们就能在软件中看到数据库的样式。这就代表着我们连接数据库成功了。
然后我们先新建一个类,在类中输入我们需要的属性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public  class  User   {    Integer userid;     String username;     Date birth;          BigDecimal salary; } 
 
然后再使用快捷键ALT加insert。打开插件。 我们可以利用这个插件使该类生成相应的SQL语句。同时也能选择是否生成索引等等。
这样生成的SQL文件就会在我们这个类文件的下面。
1 2 3 4 5 6 7 8 CREATE  TABLE  user_mybatishelper(	userid INT  (11 ) NOT  NULL  AUTO_INCREMENT COMMENT  'userid' , 	username VARCHAR  (50 ) NOT  NULL  DEFAULT  ''  COMMENT  'username' , 	birth DATETIME NOT  NULL  DEFAULT  '1000-01-01 00:00:00'  COMMENT  'birth' , 	salary DECIMAL  (13 ,4 ) NOT  NULL  DEFAULT  -1  COMMENT  '工资明细' , INDEX  `ix_username_salary` (username,salary),	PRIMARY KEY  (userid) )ENGINE =InnoDB  DEFAULT  CHARSET =utf8mb4 COMMENT  'user_mybatishelper' ; 
 
这个SQL文件再按快捷键ctrl+enter。可以执行这个文件,让他在我们的数据库生成相应的数据库表。 这样一来数据库表就生成了。
然后我们再右击选择这个数据库表。
打开这个选项。
这个选项有非常多的功能,我们只需要填好对你的路径,就能够生成所需要的mapper文件。并且还能选择是否生成服务层接口和服务层。以及是否添加实体类的注解。
选择了之后我们就能看到。
1 2 3 4 5 6 7 8 9 10 11 12 13 public  interface  UserMybatishelperMapper   {    int  deleteByPrimaryKey (Integer userid)  ;     int  insert (UserMybatishelper record)  ;     int  insertSelective (UserMybatishelper record)  ;     UserMybatishelper selectByPrimaryKey (Integer userid)  ;     int  updateByPrimaryKeySelective (UserMybatishelper record)  ;     int  updateByPrimaryKey (UserMybatishelper record)  ; } 
 
这个插件已经自动为我们生成了相应的接口,并且已经实现了基本的增加,删除,修改更新等功能。
并且,我们还能够从mapper快速跳转到目标接口。 我们打开resource。文件写好了SQL还实现了基本的功能,我们只需要调用这个接口就能实现大部分的业务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper  PUBLIC  "-//mybatis.org//DTD Mapper 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper  namespace ="com.example.mybatiscodehelper.mapper.UserMybatishelperMapper" >   <resultMap  id ="BaseResultMap"  type ="com.example.mybatiscodehelper.domain.UserMybatishelper" >                <id  column ="userid"  jdbcType ="INTEGER"  property ="userid"  />      <result  column ="username"  jdbcType ="VARCHAR"  property ="username"  />      <result  column ="birth"  jdbcType ="TIMESTAMP"  property ="birth"  />      <result  column ="salary"  jdbcType ="DECIMAL"  property ="salary"  />    </resultMap >    <sql  id ="Base_Column_List" >           userid, username, birth, salary   </sql >    <select  id ="selectByPrimaryKey"  parameterType ="java.lang.Integer"  resultMap ="BaseResultMap" >           select      <include  refid ="Base_Column_List"  />      from user_mybatishelper     where userid = #{userid,jdbcType=INTEGER}   </select >    <delete  id ="deleteByPrimaryKey"  parameterType ="java.lang.Integer" >           delete from user_mybatishelper     where userid = #{userid,jdbcType=INTEGER}   </delete >    <insert  id ="insert"  keyColumn ="userid"  keyProperty ="userid"  parameterType ="com.example.mybatiscodehelper.domain.UserMybatishelper"  useGeneratedKeys ="true" >           insert into user_mybatishelper (username, birth, salary       )     values (#{username,jdbcType=VARCHAR}, #{birth,jdbcType=TIMESTAMP}, #{salary,jdbcType=DECIMAL}       )   </insert >    <insert  id ="insertSelective"  keyColumn ="userid"  keyProperty ="userid"  parameterType ="com.example.mybatiscodehelper.domain.UserMybatishelper"  useGeneratedKeys ="true" >           insert into user_mybatishelper     <trim  prefix ="("  suffix =")"  suffixOverrides ="," >        <if  test ="username != null" >          username,       </if >        <if  test ="birth != null" >          birth,       </if >        <if  test ="salary != null" >          salary,       </if >      </trim >      <trim  prefix ="values ("  suffix =")"  suffixOverrides ="," >        <if  test ="username != null" >          #{username,jdbcType=VARCHAR},       </if >        <if  test ="birth != null" >          #{birth,jdbcType=TIMESTAMP},       </if >        <if  test ="salary != null" >          #{salary,jdbcType=DECIMAL},       </if >      </trim >    </insert >    <update  id ="updateByPrimaryKeySelective"  parameterType ="com.example.mybatiscodehelper.domain.UserMybatishelper" >           update user_mybatishelper     <set >        <if  test ="username != null" >          username = #{username,jdbcType=VARCHAR},       </if >        <if  test ="birth != null" >          birth = #{birth,jdbcType=TIMESTAMP},       </if >        <if  test ="salary != null" >          salary = #{salary,jdbcType=DECIMAL},       </if >      </set >      where userid = #{userid,jdbcType=INTEGER}   </update >    <update  id ="updateByPrimaryKey"  parameterType ="com.example.mybatiscodehelper.domain.UserMybatishelper" >           update user_mybatishelper     set username = #{username,jdbcType=VARCHAR},       birth = #{birth,jdbcType=TIMESTAMP},       salary = #{salary,jdbcType=DECIMAL}     where userid = #{userid,jdbcType=INTEGER}   </update >  
 
当然还得实际工作效果远远不止如此。我们再返回相应的接口层。我们可以自己去编写相关的描述文字就能够在mapper文件里添加相应的SQL。 作为相关的描述文字是怎么回事呢?我们来演示一遍:
findByUsernameAndUserid 
比如说输入这个寻找用户名和用户id。再右键Alt和enter便能够生成相应的业务。这种文字描述不仅具有记忆联想功能,而且还有各种语法补全。他可以很方便的实现你想要的功能。
1 2 3 4 5 6 <select  id ="findByUsernameAndUserid"  resultMap ="BaseResultMap" >   select   <include  refid ="Base_Column_List"  />    from user_mybatishelper   where username=#{username,jdbcType=VARCHAR} and userid=#{userid,jdbcType=INTEGER} </select > 
 
除此之外,我们还可以选择是否检验输入的值为空等等相关操作。
并且在此基础上还可以实现很多SQL的语法。比如while between等等相关功能。
1 2 3 4 5 6 7 <select  id ="findFirstByUseridAndUsernameBetweenOrderBySalaryDescAndUseridDesc"  resultMap ="BaseResultMap" >   select   <include  refid ="Base_Column_List"  />    from user_mybatishelper   where userid=#{userid,jdbcType=INTEGER} and username <![CDATA[>]]> #{minUsername,jdbcType=VARCHAR} and username   <![CDATA[<]]> #{maxUsername,jdbcType=VARCHAR} order by salary desc, userid desc limit 1 </select > 
 
而实现这种功能仅仅需只需要用语言去描述便可以了。并且他还在原有的基础上增加了检验,关键字是否错误,自动绑定,判空,验证值是否存在连接和依赖 并且我们还可以对单接口进行测试,我们可以右键选择它的测试,然后他就会新建一个测试类给我们。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public  class  UserMybatishelperMapperTest   {    private  static  UserMybatishelperMapper mapper;     @org .junit.BeforeClass     public  static  void  setUpMybatisDatabase ()   {         SqlSessionFactory builder = new SqlSessionFactoryBuilder().build(UserMybatishelperMapperTest.class.getClassLoader().getResourceAsStream("mybatisTestConfiguration/UserMybatishelperMapperTestConfiguration.xml"));                  mapper = builder.getConfiguration().getMapper(UserMybatishelperMapper.class , builder .openSession (true )) ;     }     @org .junit.Test     public  void  testFindByUsernameAndUserid ()  throws  FileNotFoundException  {         mapper.findByUsernameAndUserid("Jack" ,1 );     } } 
 
我们可以用在连接测试看数据库是否相关的值,或者是值传达是否错误等等。
这个插件可以极大提高我们的工作效率。
但是这种插件说到底也是对我们一些基本的功能实现而已。很多实际生活场景中使用到那些业务不仅仅只有这些。所以还是需要好好学习