spring-boot mybatis 連線MySQL

weixin_33890499發表於2017-09-04

1.mybatis1.0.0方式

1.1pom.xml配置

<!-- mysql mybatis -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

1.2application.properties配置

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

# mybatis配置檔案位置
mybatis.mapperLocations=classpath:mapper/*.xml

1.3專案目錄

domain
  pojo的存放位置
mapper
  dao的存放位置
service
  業務層
web
  控制器層

resources/mapper
  mybaties 配置層
  這個配置層需要在application.properties中進行配置
1835915-ff2878278908b72a.png
image.png

1.4OneController.java

package com.shuai.spring_boot_1.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shuai.spring_boot_1.service.UserService;

@Controller
public class OneController {
    
    @Autowired
    private UserService userService;
    
    @RequestMapping("/")
    @ResponseBody
    public String index(){
        
        userService.insert();
        
        return "hello spring boot";
    }
}
1835915-b75fd9c80f531d8a.png
image.png

1.5UserService.java

package com.shuai.spring_boot_1.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shuai.spring_boot_1.domain.User;
import com.shuai.spring_boot_1.mapper.UserMapper;

@Service
public class UserService {
    
    @Autowired
    private UserMapper userMapper;
    
    public void insert(){
        User user = new User();
        user.setId(2);
        user.setName("lisi");
        userMapper.insert(user);
        System.out.println("插入成功");
    }

}
1835915-1468f175024e9db7.png
image.png

1.6UserMapper.java

package com.shuai.spring_boot_1.mapper;

import com.shuai.spring_boot_1.domain.User;

public interface UserMapper {
    
    void insert(User user);

}
1835915-556ffffd3b45edb6.png
image.png

1.7User.java

package com.shuai.spring_boot_1.domain;

public class User {
    
    private int id;
    
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
}
1835915-119f6f48af8116f7.png
image.png

1.8App.java

package com.shuai.spring_boot_1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
    
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
1835915-7a2ec68cd38ffb68.png
image.png

1.8UserMapper.xml

<?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.shuai.spring_boot_1.mapper.UserMapper" >

  <insert id="insert" parameterType="com.shuai.spring_boot_1.domain.User" >
    insert into user (id,name)
    values (#{id}, #{name})
  </insert>
  
</mapper>
1835915-bfd677efc8e6cbea.png
image.png

1.9執行專案

執行App.java中的main方法

1.10訪問專案

http://localhost:8080/

相關文章