spring boot+mybatis操作数据库

 使用的是阿里的驱动包,数据库是mysql

首先是pom文件:

<?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.springboot</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.25</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
    </dependencies>

     <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

    </build>

</project>

然后是配置文件:application.yml  这个文件是自己创建的

spring:
  datasource:
    url: ****
    driverClassName: *****
    username: ****
    password: ****
    type: com.alibaba.druid.pool.DruidDataSource
mybatis:
  type-aliases-package: com.springboot.springboot.model
  mapper-locations: classpath:*/*.xml

springboot的入口类:

package com.springboot.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.springboot.springboot.dao")
public class SpringbootApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringbootApplication.class, args);
    }
}

model 实体类:

package com.springboot.springboot.model;

/**
 * @Title: Student
 * @Package com.springboot.springboot.model
 * * @author  邓玉龙
 * @Description:
 * @date 2018-06-15 11:05
 */

public class Student {
    private int id;
    private String name;
    private int age;
    private String sex;

    public Student(){}
    public Student(int id, String name, int age, String sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    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;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}

Mapper.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.springboot.springboot.dao.StudentMapper">
    <resultMap id="BaseResultMap" type="com.springboot.springboot.model.Student">
        <id column="ID" property="id" jdbcType="INTEGER" />
        <result column="Name" property="name" jdbcType="VARCHAR" />
        <result column="Age" property="age" jdbcType="INTEGER" />
        <result column="Sex" property="sex" jdbcType="VARCHAR" />
    </resultMap>

    <insert id="saveStudent" parameterType="com.springboot.springboot.model.Student" >
        insert into Student(ID,Name,Age,Sex)values (
        #{id,jdbcType=INTEGER},#{name,jdbcType=VARCHAR},
        #{age,jdbcType=INTEGER},#{sex,jdbcType=VARCHAR})
    </insert>


</mapper>

Dao 的接口

package com.springboot.springboot.dao;

import com.springboot.springboot.model.Student;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

/**
 * @Title: StudentMapper
 * @Package com.springboot.springboot.dao
 * * @author  邓玉龙
 * @Description:
 * @date 2018-06-15 11:16
 */


public interface StudentMapper {

    void saveStudent(Student student);
}

Service实现类和接口:

package com.springboot.springboot.service;

import com.springboot.springboot.model.Student;

/**
 * @Title: StudentService
 * @Package com.springboot.springboot.service
 * * @author  邓玉龙
 * @Description:
 * @date 2018-06-15 11:31
 */
public interface StudentService {
     void saveStudent(Student student);
}

 

package com.springboot.springboot.service;

import com.springboot.springboot.dao.StudentMapper;
import com.springboot.springboot.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Title: StudentServiceImpl
 * @Package com.springboot.springboot.service
 * * @author  邓玉龙
 * @Description:
 * @date 2018-06-15 11:31
 */

@Transactional
@Service
public class StudentServiceImpl implements StudentService{
    @Resource
    private StudentMapper studentMapper;

    public void saveStudent(Student student){
        System.out.println("保存数据开始");
        student.setId(1);
        student.setName("小美眉");
        student.setAge(18);
        student.setSex("36E");
        studentMapper.saveStudent(student);

    }
}

最后是controller层:

package com.springboot.springboot.controller;

import com.springboot.springboot.model.Student;
import com.springboot.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Title: StudentController
 * @Package com.springboot.springboot.controller
 * * @author  邓玉龙
 * @Description:
 * @date 2018-06-15 15:08
 */
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;
    @GetMapping(value="/save")
    public void sTest(){

        studentService.saveStudent(new Student());
    }

}

最后启动入口类然后访问这个方法: http://localhost:8080/save

 

https://blog.csdn.net/qq_38128218/article/details/80706447

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值