• 整合 Mybatis


    MyBatis 的前身叫 iBatis,本是 apache 的一个开源项目, 2010 年这个项目由 apache software foundation 迁移到了 google code,并且改名为 MyBatis。MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的 JDBC 代码和参 数的手工设置以及结果集的检索。MyBatis 使用简单的 XML 或注解用于配置和原始映射, 将接口和 Java 的 POJOs(Plan Old Java Objects,普通的 Java 对象)映射成数据库中的记录。
    <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.mybatis</groupId>
        <artifactId>mybatics</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <!-- 声明项目配置依赖编码格式为 utf-8 -->
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <fastjson.version>1.2.24</fastjson.version>
        </properties>
    
        <dependencies>
    
            <!--Spring Data Jpa依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    
            <!--mysql数据库驱动 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
                <version>8.0.13</version><!--$NO-MVN-MAN-VER$ -->
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
            </dependency>
    
            <!--mybatis -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.10</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    server.port =8089
    
    spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?serverTimezone=UTC&autoReconnect=true
    spring.datasource.username=root
    spring.datasource.password=admin
    spring.jpa.hibernate.ddl-auto=create-drop
    ##spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    
    mybatis.mapper-locations=classpath:mappers/*.xml
    mybatis.type-aliases-package=com.tszr.mybatis.mapper
    mybatis.configuration.map-underscore-to-camel-case=true
    spring.jpa.database=MySQL
    spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
    spring.jpa.show-sql=true
    package com.tszr.mybatis.entity;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "user")
    public class User {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer id;// 编号
        
        @Column(name = "name", unique = true, nullable = false, length = 64)
        private String name;// 用户名
        
         @Column(name = "password", nullable = false, length = 64)
        private String password;// 密码
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }
    package com.tszr.mybatis.mapper;
    
    import java.util.List;
    
    import org.apache.ibatis.annotations.Mapper;
    
    import com.tszr.mybatis.entity.User;
    
    @Mapper//指定这是一个操作数据库的mapper
    public interface UserMapper {
        List<User> findAll();
        int addUser(User user);
    }
    package com.tszr.mybatis.servies;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    import com.tszr.mybatis.entity.User;
    import com.tszr.mybatis.mapper.UserMapper;
    
    @Service
    public class UserService {
        @Autowired
        private UserMapper userMapper;
    
        public List<User> findAll() {
            return userMapper.findAll();
        }
    
        public int addUser(User user) {
            return userMapper.addUser(user);
        }
    }
    package com.tszr.mybatis.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    
    import com.tszr.mybatis.entity.User;
    import com.tszr.mybatis.servies.UserService;
    
    @RestController
    @RequestMapping("/user")
    public class UserController {
        @Autowired
        private UserService userService;
    
        @RequestMapping("/findAll")
        public List<User> findAll() {
            return userService.findAll();
        }
    
        @PostMapping("/add")
        public String add(@RequestBody User user) {
            System.out.println("******************");
            System.out.println(user.getName());
            int i = userService.addUser(user);
            if (i > 0) {
                return "success";
            }
            return "fail";
        }
    }
    <?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.tszr.mybatis.mapper.UserMapper">
        <resultMap id="UserResult" type="com.tszr.mybatis.entity.User">
            <id column="id" property="id" ></id>
            <result column="name" property="name" ></result>
            <result column="password" property="password" ></result>
        </resultMap>
        <select id="findAll" resultMap="UserResult">
            SELECT * FROM user
        </select>
        <insert id="addUser" parameterType="com.tszr.mybatis.entity.User">
            insert into user (name,password) values (#{name},#{password});
        </insert>
    </mapper>
    package com.tszr.mybatis;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

     

  • 相关阅读:
    Oracle学习
    挑战练习11.5 恢复CrimeFragment的边距
    844. Backspace String Compare
    496. Next Greater Element I
    682. Baseball Game
    707. Design Linked List
    挑战练习10.6 实现高效的RecyclerView刷新
    挑战练习9.5 日期格式化
    挑战练习8.8 RecyclerView ViewType
    挑战练习6.5 限制作弊次数
  • 原文地址:https://www.cnblogs.com/tszr/p/15450969.html
Copyright © 2020-2023  润新知