springboot+mybatis注解开发的增删改查

1.配置相关:

server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/db_mybatis?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver



spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

mybatis.type-aliases-package=cn.sjxy.springboot.mybatis_study.domain
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true

2.主配置类

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@MapperScan(value = {"cn.sjxy.springboot.mybatis_study.mapper"})
@SpringBootApplication
public class MybatisStudyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisStudyApplication.class, args);
    }

}

3.pom文件

  <properties>
        <java.version>1.8</java.version>
        <mapper.starter.version>2.0.3</mapper.starter.version>
        <pageHelper.starter.version>1.2.5</pageHelper.starter.version>
        <mapper.version>3.3.9</mapper.version>
        <swagger.version>2.7.0</swagger.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-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

        <!-- 通用Mapper启动器 -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>${mapper.starter.version}</version>
        </dependency>

        <!-- 分页助手启动器 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>${pageHelper.starter.version}</version>
        </dependency>

        <!-- 阿里巴巴 json -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!--swagger2-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.9</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.11</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!--不打包测试文件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>

4.swagger2配置,综合我上一篇的swagger2进行配置
5.实体类
@Data可以自动生成get,set,tostring方法
需要安装一个插件
在这里插入图片描述
@ApiModel(“员工信息”)是对实体类的描述

@Data
@ApiModel("员工信息")
public class Employee {
    private int id;
    private String name;
    private Integer age;
    private int deptId;
    private Department department;
}
@Data
@ApiModel("部门信息")
public class Department {

    private int id;
    private String name;
    private List<Employee> employee;


}

6.dao层编写

public interface EmployeeMapper {
//查询emp表所有信息,默认的结果集是 EMployee这个实体类
    @Select("select * from emp")
    List<Employee> findAll();

//查询emp表中的所有信息,包括了员工对应的部门信息(多对一)
//使用one进行映射
    @Select("select * from emp")
    @Results(value = {
    //查询的主表是emp,需要查询的附属信息在dept表中,这里不能用外连接而是用查询的方法调用。
    //就是将emp表中与dept表中对应的字段作为参数传给findDept方法,column就是传的emp表中的字段
            @Result(property = "department", column = "dept_id",one = @One(select = "cn.sjxy.springboot.mybatis_study.mapper.DepartmentMapper.findDept"))
    })
    List<Employee> findAllWithDept();


//查询一个员工的信息包括所在部门(一对一)
    @Select(" SELECT " +
            " * " +
            " FROM" +
            " emp" +
            " where" +
            " id = #{id} ")
    @Results(value = {
            @Result(property = "department", column = "dept_id", one = @One(select = "cn.sjxy.springboot.mybatis_study.mapper.DepartmentMapper.findDept"))
    })
    Employee findById(@Param("id") int id);

//通过部门编号查询员工
    @Select(" select " +
            " * " +
            " from " +
            " emp " +
            " where dept_id = #{dept_id}")
//    @Results(value = {
//            @Result(property = "department", column = "dept_id", one=@One(select = "cn.sjxy.springboot.mybatis_study.mapper.DepartmentMapper.findDept"))
//    })
    List<Employee> findByDeptId(@Param("dept_id")int dept_id);

//通过编号删除员工
    @Delete("DELETE" +
            " FROM " +
            " emp " +
            " WHERE " +
            " id=#{id}")
    void deleteById(@Param("id") int id);

//插入员工
    @Insert(" INSERT " +
            " INTO " +
            " emp(name,age,dept_id) " +
            " VALUES(#{name}, #{age}, #{deptId}) ")
    void insert(Employee employee);

//更新员工信息
    @Update(" UPDATE " +
            " emp " +
            " SET name=#{name}, age = #{age} " +
            " WHERE " +
            " id = #{id}")
    void update(Employee employee);


}
public interface DepartmentMapper {

//通过部门id查询部门信息
    @Select(" select * from dept where id = #{id}")
    List<Department> findDept(@Param("id") int id);

//查询所有部门信息,包括部门下的员工信息(一对多)
    @Select(" select " +
            " * " +
            " from " +
            "dept")
    @Results(value = {
            @Result(property = "id", column = "id"),
            //查询的主表是dept,需要从emp表中取得相应的信息,就需要将dept对应于emp表的字段作为column的值传给方法findByDeptId
            @Result(property = "employee", column = "id",
                    many = @Many(select =
                            "cn.sjxy.springboot.mybatis_study.mapper.EmployeeMapper.findByDeptId"))
    })
    List<Department> findDeptWithEmp();
}

7.service层

@Service
public class DepartmentService {
    @Resource
    private DepartmentMapper departmentMapper;

   public List<Department> findDeptWithEmp(){
        return departmentMapper.findDeptWithEmp();
    }
}
 */
@Service
public class EmployeeService {
    @Resource
    EmployeeMapper employeeMapper;

    public List<Employee> findAll(){
        System.out.println(employeeMapper.findByDeptId(1));
        return employeeMapper.findAll();
    }

    public List<Employee> findAllWithDept(){
        return employeeMapper.findAllWithDept();
    }

    public Employee findById(int id){
        return employeeMapper.findById(id);
    }

    public void deleteById(int id){
        employeeMapper.deleteById(id);
    }

    public void update(Employee employee){
        employeeMapper.update(employee);
    }

    public void insert(Employee employee){
        employeeMapper.insert(employee);
    }
}

8.controller层

@RestController
@RequestMapping("/api/emp")
@Api(tags = "员工相关")
public class EmpController {
    @Resource
    private EmployeeService employeeService;

    @ApiOperation(value = "查询所有员工")
    @GetMapping("/findAll")
    public Result findAll(){
        return new Result<>(employeeService.findAll()) ;
    }

    @ApiOperation(value = "查询所有员工及其部门")
    @GetMapping("/findAllWithDept")
    public Result findAllWithDept(){
        return new Result<>(employeeService.findAllWithDept());
    }

    @ApiOperation(value = "查询员工信息")
    @ApiImplicitParam(paramType = "query", name = "id", dataType = "int", value = "用户id",required = true, defaultValue = "1")
    @GetMapping("/findById")
    public Result findById(@RequestParam("id")int id){
        return new Result(employeeService.findById(id));
    }

    @ApiOperation(value = "删除员工")
    @GetMapping("/deleteById")
    public void deleteById(@RequestParam("id")int id){
         employeeService.deleteById(id);
    }

    @ApiOperation(value = "修改员工")
    @GetMapping("/update")
    public void update(Employee employee){
        employeeService.update(employee);
    }

    @ApiOperation(value = "添加员工")
    @GetMapping("/insert")
    public void insert(Employee employee){
        employeeService.insert(employee);
    }
}

@RestController
@RequestMapping("/api/dept")
@Api(tags = "部门相关")
public class DepartmentController {
    @Resource
    private DepartmentService departmentService;

    @ApiOperation(value = "部门信息")
    @GetMapping("/findDeptWithEmp")
    public Result findDeptWithEmp(){
        return new Result<>(departmentService.findDeptWithEmp());
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值