Spring Boot 4 MyBatis

Source: Internet
Author: User
Tags assert

Using MyBatis within Springboot, you can map by annotation without using the XML mapping configuration.

Pom.xml adding dependencies

    <Dependency>            <groupId>Org.mybatis.spring.boot</groupId>            <Artifactid>Mybatis-spring-boot-starter</Artifactid>            <version>1.1.1</version>        </Dependency>        <Dependency>            <groupId>Mysql</groupId>            <Artifactid>Mysql-connector-java</Artifactid>            <version>5.1.21</version>        </Dependency>

Adding in the Application.properties configuration file

Spring.datasource.url=jdbc:mysql://localhost:3306/test2spring.datasource.username= Rootspring.datasource.password=hongda$123456spring.datasource.driver-class-name=com.mysql.jdbc.driver

This configuration allows you to manipulate the data in the database, but the tables and databases must be created on their own, and hibernate is a little different.

Using MyBatis:

 Public classUser {PrivateLong ID; PrivateString name; PrivateInteger age;  PublicLong getId () {returnID; }     Public voidsetId (Long id) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicInteger getage () {returnAge ; }     Public voidsetage (Integer age) { This. Age =Age ; }}

Operation:

@Mapper Public Interfaceusermapper {@Select ("SELECT * from user WHERE name = #{name}") User findbyname (@Param ("Name") String name); @Results ({@Result ( property= "Name", column = "Name"), @Result ( property= ' age ', column = ' age ')}) @Select ("Select name, age from user") List<User>FindAll (); @Insert ("INSERT into User (name, age) VALUES (#{name}, #{age})")    intInsert (@Param ("name") String name, @Param ("Age") (Integer age); @Update ("UPDATE user SET Age=#{age} WHERE Name=#{name}")    voidUpdate (user user); @Delete ("DELETE from user WHERE ID =#{id}")    voidDelete (Long ID); @Insert ("INSERT into User (name, age) VALUES (#{name}, #{age})")    intinsertbyuser (user user); @Insert ("INSERT into User (name, age) VALUES (#{name,jdbctype=varchar}, #{age,jdbctype=integer})")    intInsertbymap (map<string, object>map);}

Run:

@RunWith (Springjunit4classrunner.class) @SpringApplicationConfiguration (Classes= Application.class) @Transactional Public classapplicationtests {@AutowiredPrivateUsermapper Usermapper; @Test @Rollback Public voidTestusermapper ()throwsException {//Insert a piece of data and select it to verifyUsermapper.insert ("AAA", 20); User u= Usermapper.findbyname ("AAA"); Assert.assertequals (20, U.getage (). Intvalue ()); //update a piece of data and select to verifyU.setage (30);        Usermapper.update (U); U= Usermapper.findbyname ("AAA"); Assert.assertequals (30, U.getage (). Intvalue ()); //Delete this data, and select VerifyUsermapper.delete (U.getid ()); U= Usermapper.findbyname ("AAA"); Assert.assertequals (NULL, u); U=NewUser ("BBB", 30);        Usermapper.insertbyuser (U); Assert.assertequals (Usermapper.findbyname ("BBB")). Getage (). Intvalue ()); Map<string, object> map =NewHashmap<>(); Map.put ("Name", "CCC"); Map.put ("Age", 40);        Usermapper.insertbymap (map); Assert.assertequals (Usermapper.findbyname ("CCC"). Getage (). Intvalue ()); List<User> userlist =Usermapper.findall ();  for(User user:userlist) {assert.assertequals (NULL, User.getid ()); Assert.assertnotequals (NULL, User.getname ()); }    }}

http://blog.didispace.com/mybatisinfo/

Spring Boot 4 MyBatis

Related Article

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.