美文网首页
(二)spring boot 整合 mybatis

(二)spring boot 整合 mybatis

作者: iarchitect | 来源:发表于2018-09-14 17:50 被阅读0次

    开发环境:
    java version "1.8.0_171"
    Apache Maven 3.5.4
    mysql

    在数据库中创建表

    CREATE TABLE `user` (
      `id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'ID',
      `name` varchar(255) NOT NULL COMMENT '用户名',
      `age` int(10) NOT NULL COMMENT '年龄',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
    
    

    在IDEA中创建新项目,直接一路next到下图,选择这两项之后继续直接一路next然后finish


    image.png

    项目结构如下图所示:


    image.png

    然后application.properties文件中配置数据库相关链接信息

    spring.datasource.url=jdbc:mysql://localhost:3306/test
    spring.datasource.username=root
    spring.datasource.password=
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    

    创建UserMapper接口

    package com.example.demo.dao.mapper;
    
    import com.example.demo.dao.model.User;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Select;
    import org.springframework.stereotype.Component;
    
    @Mapper
    @Component(value = "userMapper")
    public interface UserMapper {
    
        @Select("SELECT * FROM USER WHERE NAME = #{name}")
        User findByName(@Param("name") String name);
    
        @Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")
        int insert(@Param("name") String name, @Param("age") Integer age);
    
    }
    
    

    创建Dao类

    package com.example.demo.dao.model;
    
    public class User {
        private Long id;
        private String name;
        private Integer age;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    }
    
    

    在测试类上添加测试方法

    package com.example.demo;
    
    import com.example.demo.dao.mapper.UserMapper;
    import com.example.demo.dao.model.User;
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.annotation.Rollback;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class DemoApplicationTests {
    
        @Test
        public void contextLoads() {
        }
    
        @Autowired
        private UserMapper userMapper;
    
        @Test
        public void findByName() throws Exception {
            userMapper.insert("AAA", 20);
            User u = userMapper.findByName("AAA");
            System.out.println(u.getName());
            Assert.assertEquals(20, u.getAge().intValue());
        }
    
    }
    
    

    运行findByName方法:


    image.png

    附件:pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.2</version>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    
    </project>
    
    

    相关文章

      网友评论

          本文标题:(二)spring boot 整合 mybatis

          本文链接:https://www.haomeiwen.com/subject/cqzogftx.html