springboot+mybatis+MySQL(入门级-半小时搞定系列)

入门级-半小时搞定系列

非常简易的demo

https://start.spring.io/ 

在这个网站可以按照自己所需的依赖,自动生成一个springboot项目的压缩包,解压即可。

 

非常简单的方法

 

 

application.properties

server.port=8080
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=2999029aaa

mybatis.type-aliases-package=com.domain
View Code

PersonDao.java

package com.dao;
import com.domain.Person;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface PersonDao {
    @Select(" select * from person")
    List<Person> query();
    @Select("select age,name,id from person where id=#{id}")
    Person queryid(int id);
    @Insert("insert into person(age, name, id)\n" +"values (#{age},#{name},#{id})")
    int insertPerson(Person person);
    @Update("update person\n" +
            "    set age = #{age} ,name = #{name}\n" +
            "    where id = #{id}")
    int updatePerson(Person person);
    @Delete("delete from person where id = #{id,jdbcType=VARCHAR}")
    int deletePerson(Person person);
}
View Code

Person.java

package com.domain;

import java.io.Serializable;

/**
 * person
 * @author 
 */
public class Person implements Serializable {
    private String id;

    private Integer age;

    private String name;

    private static final long serialVersionUID = 1L;

    public String getId() {
        return id;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
View Code

PersonService.java

package com.service;

import com.domain.Person;

import java.util.List;

public interface PersonService {
    List<Person> query();
    Person queryid(int id);
    int insertPerson(Person person);
    int updatePerson(Person person);
    int deletePerson(Person person);
}
View Code

PersonServiceimpl.java

package com.service;

import com.dao.PersonDao;
import com.domain.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.List;
@EnableTransactionManagement//开启事务管理器
@Service
public class PersonSeriviceimpl implements PersonService{
    @Autowired
    PersonDao personDao;
    @Override
    public List<Person> query() {
        return personDao.query();
    }

    @Override
    public Person queryid(int id) {
        return personDao.queryid(id);
    }

    @Override
    public int insertPerson(Person person) {
        personDao.insertPerson(person);
        return 0;
    }

    @Override
    public int updatePerson(Person person) {
        personDao.updatePerson(person);
        return 0;
    }

    @Override
    public int deletePerson(Person person) {
        personDao.deletePerson(person);
        return 0;
    }
}
View Code

PersonController.java

package com.controller;
import com.service.PersonService;
import com.domain.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Transactional
@RestController
public class PersonController {
    @Autowired
    PersonService personService;
    @RequestMapping("/insert")
    public String insertPerson(Person person){
        personService.insertPerson(person);
        return "ok";
    }
    @RequestMapping("/id")
    public Person queryid(int id){
        Person person=personService.queryid(id);
        return person;
    }
    @RequestMapping("/query")
    public List<Person> query(){
        List<Person>people=personService.query();
        return people;
    }
}
View Code

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>

        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>
View Code

 

 

输入网址,验证:

 

 

 

posted @ 2020-03-22 14:21  zuiaimiusi  阅读(321)  评论(0编辑  收藏  举报