Springboot (v): springboot integration Mybatis__springboot

Source: Internet
Author: User

How to say, although blogging is a very time-consuming thing, but also a rookie, but if the writing can help others, it is worth happy.

Review:

The article wrote a jdbctemplate, but think of the use of MYBATIS,JPA people estimate a lot, so this article to write about Springboot integration MYBATIS,JPA then have time to do, because they have no use. I. Data Preparation

In fact, the last article, in case someone is directly read this article, it is still posted it.

CREATE TABLE ' tb_user ' (
  ' id ' int (one) not NULL auto_increment COMMENT ' id ',
  ' username ' varchar () NOT NULL commen  T ' username ',
  ' age ' int (one) NOT null COMMENT ' ages ',
  ' CTM ' datetime NOT NULL COMMENT ' creation time ',
  PRIMARY KEY (' id ')
) Engine=innodb DEFAULT Charset=utf8
INSERT into ' db_test '-' tb_user ' (' username ', ' age ', ' CTM ') VALUES (' John ', ' ', ' "Now ');
INSERT into ' db_test '-' tb_user ' (' username ', ' age ', ' CTM ') VALUES (' Dick ', ' ', ', "Now ');
INSERT into ' db_test '-' tb_user ' (' username ', ' age ', ' CTM ') VALUES (' Harry ', ' ', ' and now ());
Ii. introduction of Reliance
		<!--spring-mybatis-->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.0</version>
	        </dependency>

		<!--MySQL-->
		<dependency>
			<groupid>mysql</groupid >
			<artifactId>mysql-connector-java</artifactId>
		</dependency>

Also, web dependencies are needed because we use the MVC pattern.

	<!--Add Typical dependencies for a Web application-->
        <dependency>
            <groupId> org.springframework.boot</groupid>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
third, the database configuration file
Spring:
  DataSource:
    driver-class-name:com.mysql.jdbc.driver
    url:jdbc:mysql://localhost:3306/db_ User
    Username:root
    password:root
Four, code

Getting Started with Mybati is a hassle, with a variety of profiles, entity classes, DAO layer mapping associations, and a big push for other configurations. Of course, MyBatis also found that the drawbacks, initially developed generator can be based on table results automatically produce entity classes, configuration files and DAO layer code, can reduce the amount of development, the latter also carried out a lot of optimization can use annotations.

So the two ways of use are introduced, one, no configuration note Version II, configuration file version 1. No profile annotation version

To join a configuration file

MyBatis:
  Type-aliases-package:cn.saytime.bean

Entity class User.class

Package Cn.saytime.bean;

Import java.util.Date;

/**
 * @ClassName cn.saytime.bean.User
 * @Description
 * @date 2017-07-04 22:47:28/public
class User {

	private int id;
	Private String username;
	private int age;
	Private Date CTM;

	Public user () {
	} public

	User (String username, int age) {
		this.username = Username;
		This.age = age;
		THIS.CTM = new Date ();
	}

	Getter, Setter
}

Usermapper.class

Package cn.saytime.mapper;
Import Cn.saytime.bean.User;
Import Org.apache.ibatis.annotations.Delete;
Import Org.apache.ibatis.annotations.Insert;
Import Org.apache.ibatis.annotations.Param;
Import Org.apache.ibatis.annotations.Select;

Import Org.apache.ibatis.annotations.Update;

Import java.util.List; @Mapper can use the @mapper annotation here, but each Mapper annotation is more troublesome, so the unified configuration @mapperscan in the scan path in the Application class public interface Usermapper {@

	Select ("SELECT * from tb_user WHERE id = #{id}") User Getuserbyid (Integer ID);

	The @Select ("Select * from Tb_user") is public list<user> getuserlist ();

	@Insert ("Insert into Tb_user (username, age, CTM) VALUES (#{username}, #{age}, now ())") public int Add (user user); @Update ("update tb_user SET username = #{user.username}, age = #{user.age} WHERE id = #{id}") public int Update (@Param ("

	ID ") Integer ID, @Param (" user ") user user); @Delete (' Delete from tb_user where id = #{id} ') public int Delete (Integer ID);}

MyBatis Annotations Using http://www.mybatis.org/mybatis-3/zh/java-api.html

Userservice.class

Package cn.saytime.service;

Import Cn.saytime.bean.User;
Import Org.springframework.stereotype.Service;

Import java.util.List;

/**
 * @ClassName cn.saytime.service.UserService
 * @Description/public
interface UserService {

	User Getuserbyid (Integer ID);

	Public list<user> getuserlist ();

	public int Add (user user);

	public int update (Integer ID, user user);

	public int Delete (Integer ID);
}

Userserviceimpl.class

Package Cn.saytime.service.impl;
Import Cn.saytime.bean.User;
Import Cn.saytime.mapper.UserMapper;
Import Cn.saytime.service.UserService;
Import org.springframework.beans.factory.annotation.Autowired;

Import Org.springframework.stereotype.Service;

Import java.util.List; /** * @ClassName Cn.saytime.service.impl.UserServiceImpl * @Description/@Service public class Userserviceimpl Implem

	Ents userservice {@Autowired private usermapper usermapper;
	@Override public User Getuserbyid (Integer ID) {return Usermapper.getuserbyid (ID);
	@Override public list<user> getuserlist () {return usermapper.getuserlist ();
	@Override public int Add (user user) {return usermapper.add (user);
	@Override public int update (Integer ID, user user) {return usermapper.update (ID, user);
	@Override public int Delete (Integer ID) {return usermapper.delete (ID); }
}

Jsonresult.class Universal JSON return class

Package Cn.saytime.bean;

public class Jsonresult {

	private String status = NULL;

	Private Object result = null;

	Public Jsonresult status (String status) {
		this.status = status;
		return this;
	}

	Getter Setter
}

Usercontroller.class (restful style)

package Cn.saytime.web;
Import Cn.saytime.bean.JsonResult;
Import Cn.saytime.bean.User;
Import Cn.saytime.service.UserService;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.http.HttpStatus;
Import org.springframework.http.ResponseEntity;
Import org.springframework.web.bind.annotation.PathVariable;
Import Org.springframework.web.bind.annotation.RequestBody; Import ORG.SPRINGFRAMEWORK.WEB.BIND.ANNOTATION.R 

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.