• springboot2 和mybatis增删改查.注解版本


    参考链接 https://baijiahao.baidu.com/s?id=1653043822915271722

    结构图

    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.fdd</groupId>
    	<artifactId>mybatis_at</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<name>mybatis_at</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.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web-services</artifactId>
    		</dependency>
    		
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.mybatis.spring.boot</groupId>
    			<artifactId>mybatis-spring-boot-starter</artifactId>
    			<version>2.1.1</version>
    		</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>
    

    配置属性文件

    src/main/resources/application.properties

    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    spring.datasource.username=root
    spring.datasource.password=
    
    mybatis.type-aliases-package=com.fdd.mybatis.dao

     对应的数据表

    CREATE TABLE `person` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(255) DEFAULT NULL,
      `age` int(3) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

      1.编写 dao层

    src/main/java/com/fdd/mybatis/dao/Person.java

    package com.fdd.mybatis.dao;
    public class Person
    {
    	private int id;
    	private String name;
    	private int age;
    	
    	public Person()
    	{
    		
    	}
    	public Person(int id,String name,int age)
    	{
    		this.id = id;
    		this.name = name;
    		this.age = age;
    	}
    	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 toString()
    	{
    		return "person{id="+this.id+",name="+this.name+",age="+this.age+"}";
    	}
    }
    

      2.编写业务注解

    src/main/java/com/fdd/mybatis/mapper/PersonMapper.java

    package com.fdd.mybatis.mapper;
    
    import java.util.List;
    
    import org.apache.ibatis.annotations.Delete;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Select;
    import org.apache.ibatis.annotations.Update;
    
    import com.fdd.mybatis.dao.Person;
    
    public interface PersonMapper
    {
        @Insert("insert into person(name,age) values(#{name},#{age})")
        int insert(Person person);
        
        @Delete("delete from person where id = #{id}")
        int deleteByPrimaryKey(Integer id);
        
        @Update("update person set name=#{name},age=#{age} where id = #{id}")
        int updateByPrimaryKey(Person person);
        
        @Select("select id,name,age from person")
        List<Person> selectAllPerson();
        
        @Select("select id,name,age from person where id = #{id}")
        List<Person> selectOne(Integer id);
        
    
    }

    3.编写服务层

    src/main/java/com/fdd/mybatis/service/PersonService.java

    package com.fdd.mybatis.service;
    
    import java.util.List;
    
    import com.fdd.mybatis.dao.Person;
    
    public interface PersonService
    {
        int insertPerson(Person person);
        
        int deleteByPersonId(Integer id);
        
        int updateByPersonId(Person record);
        
        
        List<Person> selectAllPerson();
        
        List<Person> selectOne(Integer id);
        
    }

    src/main/java/com/fdd/mybatis/service/PersonServiceImpl.java

    package com.fdd.mybatis.service;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import com.fdd.mybatis.dao.Person;
    import com.fdd.mybatis.mapper.PersonMapper;
    
    @Service
    public class PersonServiceImpl implements PersonService
    {
        @Autowired
        private PersonMapper personMapper;
        
    
        
        @Override
        public int insertPerson(Person person)
        {
            return personMapper.insert(person);
        }
        
        @Override
        public int deleteByPersonId(Integer id)
        {
            return personMapper.deleteByPrimaryKey(id);
        }
        
        public int updateByPersonId(Person person)
        {
            return personMapper.updateByPrimaryKey(person);
        }
        
    
        
        public List<Person> selectAllPerson()
        {
            return personMapper.selectAllPerson();
        }
        
        public List<Person> selectOne(Integer id)
        {
            return personMapper.selectOne(id);
        }
    }

    4.编写请求控制器

    src/main/java/com/fdd/mybatis/controller/PersonController.java

    package com.fdd.mybatis.controller;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.fdd.mybatis.dao.Person;
    import com.fdd.mybatis.service.PersonService;
    
    @RestController
    @RequestMapping("person")
    public class PersonController
    {
        @Autowired
        private PersonService personService;
        
        @RequestMapping(value="/findAll")
        public String findAll()
        {
            List<Person> people = personService.selectAllPerson();
            people.stream().forEach(System.out::println);
            return people.toString();
        }
        
        @RequestMapping(value="/findOne/{id}")
        public List<Person> findOne(@PathVariable int id)
        {
            List<Person> people = personService.selectOne(id);
            return people;
        }
        
        @RequestMapping(value="/deleteData/{id}")
        public int deleteData(@PathVariable int id)
        {
            return personService.deleteByPersonId(id);
        }
        
        @RequestMapping(value="/addData",method = RequestMethod.POST)
        public int addData(@RequestBody Person person)
        {
            Person people = new Person();
            people.setAge(person.getAge());
            people.setName(person.getName());
            return personService.insertPerson(people);
        }
        
        @RequestMapping(value="/updateData",method=RequestMethod.POST)
        public int updateData(@RequestBody Person person)
        {
            
            Person people = new Person();
            people.setAge(person.getAge());
            people.setId(person.getId());
            people.setName(person.getName());
            return personService.updateByPersonId(people);
        }
        
    
    }

    5.入口文件添加bean扫描

    src/main/java/com/fdd/mybatis/MybatisAtApplication.java

    package com.fdd.mybatis;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    @MapperScan("com.fdd.mybatis.mapper")
    public class MybatisAtApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MybatisAtApplication.class, args);
        }
    
    }
  • 相关阅读:
    数据库-第十章 数据库恢复技术-10.8 小结
    数据库-第十章 数据库恢复技术-10.7 数据库镜像
    数据库-第十章 数据库恢复技术-10.6 具有检查点的恢复技术
    SpringCloud系列之集成分布式事务Seata应用篇
    Redis数据迁移同步工具(redis-shake)
    SpringCloud系列之集成Dubbo应用篇
    SpringCloud系列之网关(Gateway)应用篇
    SpringCloud系列之服务注册发现(Eureka)应用篇
    SpringCloud系列之配置中心(Config)使用说明
    webService-axis开发jar包
  • 原文地址:https://www.cnblogs.com/lin3615/p/12419205.html
Copyright © 2020-2023  润新知