Easymybatis Quick Start (springboot)

Source: Internet
Author: User
Tags add time zip

This article mainly explains the use of Springboot rapid integration of Easymybatis. Because Easymybatis has its own spring-boot-starter, a new project can be very fast, as fast as spring data JPA. Create a Springboot project

Access http://start.spring.io/generate a springboot empty project, enter Group,artifact point generation, as shown in figure:

Click Generate Project to download the Demo.zip Import Project

Unzip the downloaded Demo.zip, and then import the project. Right-click Import ...-Existing Maven project in Eclipse, select the project folder. Wait for the MAVEN-related jar package to download after importing into eclipse. Add maven Dependency

After the jar package is downloaded, open Pom.xml and add the following dependencies:

<!--Easymybatis Starter--
<dependency>
    <groupid>net.oschina.durcframework</ groupid>
    <artifactId>easymybatis-spring-boot-starter</artifactId>
    <version>1.8.1 </version>
</dependency>
<dependency>
    <groupid>org.springframework.boot </groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactid>mysql-connector-java</ artifactid>
    <scope>runtime</scope>
</dependency>
Add Database Configuration

Adding a database configuration in Application.properties

Spring.datasource.driver-class-name=com.mysql.jdbc.driver
Spring.datasource.url=jdbc:mysql://localhost : 3306/stu?useunicode=true&characterencoding=utf-8&zerodatetimebehavior=converttonull
Spring.datasource.username=root
Spring.datasource.password=root
adding Java files

Suppose there is a t_user table in the database with the following DDL:

CREATE TABLE ' t_user ' (
  ' id ' int (one) not NULL auto_increment COMMENT ' id ',
  ' username ' varchar (255) DEFAULT NULL CO Mment ' username ',
  ' state ' tinyint (4) default null COMMENT ' status ',
  ' Isdel ' bit (1) default null COMMENT ' delete ',
  ' Remar K ' text COMMENT ' remarks ',
  ' add_time ' datetime DEFAULT NULL COMMENT ' Add time ',
  PRIMARY KEY (' id ')
) Engine=innodb DE FAULT charset=utf8 comment= ' user table ';

We add the corresponding entity classes and DAO:
-Tuser.java:

@Table (name = "T_user") public
class TUser {    
    @Id
    @Column (name= "Id")
    @GeneratedValue (strategy = generationtype.identity)
    private Integer ID;//ID   
    private String username;//user name 
    private Byte State;//Status 
  private Boolean Isdel;  Whether to delete the 
    private String remark;  Note   
    private Date addtime;   Add time

    //Omit getter Setter
}

The entity class file takes the same approach as hibernate, and you can use our code generation tools to generate
Https://gitee.com/durcframework/easymybatis-generator Tuserdao.java:

Public interface Tuserdao extends cruddao<tuser> {
}

Tuserdao inherits Cruddao, so this DAO has a crud function. Adding test Cases

public class Tuserdaotest extends demoapplicationtests {@Autowired Tuserdao dao;
        @Test public void Testinsert () {TUser user = new TUser ();       
        User.setisdel (FALSE);       
        User.setremark ("Testinsert");

        User.setusername ("Zhang San");

        Dao.save (user);       
    SYSTEM.OUT.PRINTLN ("added primary key:" + User.getid ());
        } @Test public void Testget () {TUser user = Dao.get (3);                
    SYSTEM.OUT.PRINTLN (user);
        } @Test public void Testupdate () {TUser user = Dao.get (3);     
        User.setusername ("John Doe");

        User.setisdel (TRUE);
        int i = dao.update (user);
    System.out.println ("testupdate--" + i);
        } @Test public void Testdel () {TUser user = new TUser ();
        User.setid (3);
        int i = Dao.del (user);
    System.out.println ("Del--and" + i); }
}

Then run the unit test, and after successful operation, the project is finished.

Final Project Structure chart:

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.