Generic DAO MyBatis Package, Package general additions and deletions (III.)

Source: Internet
Author: User
Tags foreach bind stub table name

Have published two articles about the MyBatis package, at that time had promised to test no problem after a Hai will tidy up and then release the original code.
So today is just a friend to find me a copy of that set of MyBatis package of source code, I have to organize a copy, think so long time and did not find any obvious bug, so decided to send it out.

Favorite friends can download here:
Http://aiyiupload.oss-cn-beijing.aliyuncs.com/blog/img/2016/06/28/15/6d69ad50-ab53-4f4f-b4e7-1fed010bfdb9.rar

As for the MyBatis package, I called him a crud framework, which was a bit more easy to say. This is a MAVEN project and the code is in Src/main/java. Configuration file in Src/main/resource, this is not much to say, the entire CRUD source code in the Com.aiyi.core package.

Like friends can go in and see, there is no advanced technology, is to write a common DAO and proxy the implementation layer of DAO. There is com.aiyi.base bag, is you write code place, there are some DAO package, service package, controller package and so on. This is something I write casually. Some examples of use, these are based on the structure of their own projects.

And then there are some ways to use it. After importing the project into Eclipse, go to the Com.aiyi.base.dao package and see the two entity classes that correspond to the two tables in the database.
Testuserpo.java:

package Com.aiyi.base.pojo;
Import Com.aiyi.core.annotation.po.FieldName;
Import Com.aiyi.core.annotation.po.TableName;
Import Com.aiyi.core.annotation.po.TempField;

Import Com.aiyi.core.beans.Po;

 @TableName used to represent the table name in the corresponding database of the current entity class @TableName (name= "Q_test_table1") public class Testuserpo extends Po {private int id; @FieldName annotation is used to identify the database field name that corresponds to the field.

 This annotation is not required if the database field and the field in the entity class are consistent @FieldName (name= "img_id") private int imgid;

 private String name;
 @TempField Annotation Identification Imgurl This field is a temporary field, that is, the field does not exist in the database.

 @TempField private String Imgurl;
 public int getId () {return id;
 } public void setId (int id) {this.id = ID;
 } public int Getimgid () {return imgid;
 } public void Setimgid (int imgid) {this.imgid = Imgid;
 } public String GetName () {return name;
 } public void SetName (String name) {this.name = name;
 } public String Getimgurl () {return imgurl;
 } public void Setimgurl (String imgurl) {this.imgurl = Imgurl; }


}

Testimgresourcepo.java:

Package Com.aiyi.base.pojo;

Import Com.aiyi.core.annotation.po.TableName;
Import Com.aiyi.core.beans.Po;

@TableName (name= "Q_test_table2") public
class Testimgresourcepo extends Po {

 private int id;

 Private String URL;

 public int getId () {
  return ID;
 }

 public void setId (int id) {
  this.id = ID;
 }

 Public String GetUrl () {
  return URL;
 }

 public void SetUrl (String url) {
  this.url = URL;
 }

}

In the Com.aiyi.base.dao package, there are qualifying classes, respectively, Userdao, Imgresourcedao, and their interfaces. Here if you just want to use crud to do development, the general situation is not to write the two interface classes, that is, you only need to write Userdao, Imgresourcedao these two classes just fine, but here is an example, then say the whole point, In general, although you do not need to write the DAO interface class, but I like the second class situation.

If you ask me why I have to idle to write to him interface play, because I encapsulated this set of CRUD support programming extension. In other words, you can expand the functionality as needed when you develop it. You can understand it as something called a "plugin". I'm going to have to insert 3,000 data into each of the two tables, but the original CRUD framework can insert data, but he can only insert one piece, although you could write a lovely for loop, but the efficiency is there, and if you want to insert some data in bulk, you might want to add a feature to it, In other words, write to him a plugin that supports batch add records.

So please watch, the next believe in the actual development will often encounter, I give him to add batch new support, first set up an interface class and give him life an Add method is used to add data in bulk:

Package Com.aiyi.base.dao;

Import java.util.List;

Import Com.aiyi.base.pojo.TestUserPo;

/**
 * Userdao interface class, also belongs to the love Easy crud plug-in extension class. This class is normally not created. When you need to reload or add some methods of the REUD framework, you can declare the interface class
 * This interface class overloads the Add () method, so that the Add method can insert multiple data at one time, greatly improving the efficiency of data batch increase
 * @author Guo Shenkai
 * @time June 28, 2016 PM 12:51:29
 * @email 719348277@qq.com * * */public
interface Userdaointerface {

 int Add (list<testuserpo> List);
}

After the interface class is established, I need to write a mapper mapping file, map it to execute SQL, then I build a com.aiyi.base.mapper.userDaoMapper.xml:
and point its namespace to the interface we wrote earlier: Com.aiyi.base.dao.UserDaoInterface

<?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.aiyi.base.dao.UserDaoInterface" >

    <!--reload Add--
 <insert id= "User_addlist" usegeneratedkeys= "true" parametertype= "Java.util.List" >
  insert INTO Q_test_table1 (img_id, Name)
  values
  <foreach collection= "list" item= "item" index= "index"
   separator= "," >
   (#{ Item.imgid}, #{item.name})
  </foreach>

 </insert>

</mapper>

Next build Userdao.java, and inherit Daoimpl,daoimpl has two generics, the first one is the current DAO corresponding entity class, the second is the primary key type in the current entity class. This time, your this userdao already has the general to add and delete to change the method but I hope he has the batch new method, so I am getting him to implement the Userdaointerface this class.

Package Com.aiyi.base.dao;

Import java.util.List;

Import Javax.annotation.Resource;
Import Org.mybatis.spring.SqlSessionTemplate;

Import Org.springframework.stereotype.Repository;
Import Com.aiyi.base.pojo.TestUserPo;

Import Com.aiyi.core.dao.impl.DaoImpl; /** * Userdao's implementation class, which inherits the original method of the public DAO.
 When you want to add new features to the REUD framework, you don't have to change the source code, and love Easy CRUD provides a programmable plug-in interface scheme. * You can write an interface class and implement it to extend the love crud * @author Guo Shenkai * @time June 28, 2016 afternoon 12:56:42 * @email 719348277@qq.com * */@Repository PU Blic class Userdao extends Daoimpl<testuserpo, integer> implements Userdaointerface/* Userdaointerface This interface is a demonstration of a plug-in implementation.

 He perfected the bulk-add scheme for the love-easy crud */{@Resource private sqlsessiontemplate sqlsessiontemplateass; /** * Reload the Add method, bulk Add * @param list * @return */public int Add (list<testuserpo> list) {Long Startid = Next

  Id ();
  for (int i = 0; i < list.size (); i++) {List.get (i). SetId (integer.valueof (startid + i + "")); }//Note that the "user_addlist" here corresponds to the ID of the Insertsql block in Userdaomapper.xml return SqlsessiontemplAteass.insert ("user_addlist", list); }
}

OK, so a batch of new extension classes has been written to complete. All you need to do is call the Userdao.add () method, pass in a list, and he will insert the elements from the list into the database in the shortest time possible.
Next The Imgdao page adds a batch new feature, as above, so I'm not going to post the code anymore. Look, I'll write a service to test it.

Package Com.aiyi.base.service.impl;
Import java.util.ArrayList;

Import java.util.List;

Import Javax.annotation.Resource;
Import Org.springframework.stereotype.Service;
Import org.springframework.transaction.annotation.Isolation;

Import org.springframework.transaction.annotation.Transactional;
Import Com.aiyi.base.dao.ImgDao;
Import Com.aiyi.base.dao.UserDao;
Import Com.aiyi.base.pojo.TestImgResourcePo;
Import Com.aiyi.base.pojo.TestUserPo;
Import Com.aiyi.base.service.TestService;
Import Com.aiyi.core.beans.Method;
Import com.aiyi.core.sql.where.c;
Import Com.aiyi.core.util.Formatter;

Import COM.AIYI.CORE.UTIL.FORMATTERSQL;

 @Service public class Testserviceimpl implements Testservice {@Resource private Imgdao Imgdao;

 @Resource private Userdao Userdao; Batch add user and IMG Each 3,000, the speed of the @Override @Transactional (isolation = isolation.serializable) public int addlist () {//TOD
  O auto-generated Method Stub list<testuserpo> users = new arraylist<> (); List<testImgresourcepo> IMGs = new arraylist<> ();
   for (int i = 0; i < i++) {Testimgresourcepo Imgresourcepo = new Testimgresourcepo ();
   Imgresourcepo.seturl ("http://imgresource.com/url/" + i + ". jpg");
  Imgs.add (Imgresourcepo);

  } int add = Imgdao.add (IMGs);
   for (int i = 0; i < i++) {Testuserpo Testuserpo = new Testuserpo ();
   Testuserpo.setimgid (Imgs.get (i). GetId ());
   Testuserpo.setname ("user" + i);
  Users.add (TESTUSERPO);


  } Add + = Userdao.add (users);
 return add; }//Union table query, previously said that the user entity class Imgurl is a temporary field, his real value in the IMG table URL field, the user in the imgid corresponding to the IMG table ID @Override public List<testuserpo
  > Listuser () {//TODO auto-generated method stub Formatter FMT = new Formattersql (); FMT.ADDFMT ("Imgurl", "url", Testimgresourcepo.class, Method.where ("[FMT]. R].id ", C.eq," [FMT.

  l].img_id "));
 Return Userdao.listformat (Method.createdefault (), FMT); }
}

Write a controller to run:

Package Com.aiyi.base.controller;
Import Java.util.HashMap;
Import java.util.List;

Import Java.util.Map;

Import Javax.annotation.Resource;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.ResponseBody;
Import Com.aiyi.base.pojo.TestUserPo;

Import Com.aiyi.base.service.TestService;

 @Controller public class TestController {@Resource private testservice testservice; /** * Add Test * @return */@RequestMapping ("Testadd") @ResponseBody public Object testaddlist () {Long T = System.
  Currenttimemillis ();
  int n = testservice.addlist ();

  Long TT = System.currenttimemillis ();

  Double s = (tt-t)/1000;
 Return "Total Add" + N + "bar data, time" + S + "second"; }/** * Query Test * @return */@RequestMapping ("Testselect") @ResponseBody public Object testselectfmt () {Long T
  = System.currenttimemillis ();
  list<testuserpo> Listuser = Testservice.listuser (); Long TT= System.currenttimemillis ();

  Double s = (tt-t)/1000;

  map<string, object> map = new hashmap<> ();
  Map.put ("MSG", "Total query" + listuser.size () + "entry record, time" + S + "second");

  Map.put ("OData", Listuser);

 return map; }
}

Above is this appearance, more things can be seen from my previous articles, or download the source of their own to explore, here is the introduction of so many so far, goodbye.
Uh.. Wait, there's one more thing to forget, the database is configured as its own under/crud2/src/main/resources/jdbc.properties.
Here are two test tables for MySQL:

/* Navicat MySQL Data Transfer Source server: * * * Source server version:50518 source Host: ***:3306 Source Database: * * Target server type:mysql target server version:50518 File encoding:65001 D

ate:2016-06-28 16:38:37 */SET foreign_key_checks=0; --------------------------------Table structure for q_test_table1------------------------------DROP table IF EXISTS
' Q_test_table1 '; CREATE TABLE ' q_test_table1 ' (' id ' int (one) not null auto_increment, ' name ' varchar (255) DEFAULT NULL, ' img_id ' int (

One) default NULL, PRIMARY KEY (' id ')) engine=innodb auto_increment=12001 default Charset=utf8; --------------------------------Table structure for q_test_table2------------------------------DROP table IF EXISTS
' Q_test_table2 '; CREATE TABLE ' q_test_table2 ' (' id ' int (one) not null auto_increment, ' url ' varchar (255) DEFAULT NULL, PRIMARY KEY (' Id ')) engine=innodb auto_increment=21001 DEFAULT Charset=utf8;

In addition, git address: https://git.coding.net/shengkai/crud.git
If you encounter problems during use, you are welcome to submit the solution. and the perfect code.
OK. It's really good to see you here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.