SpringBoot学习笔记8 - SpringBoot中使用MyBatis

1、添加必要依赖


<dependencies>
	...
	<!-- mybatis 与 spring boot 整合依赖 -->
	<dependency>
		<groupId>org.mybatis.spring.boot</groupId>
		<artifactId>mybatis-spring-boot-starter</artifactId>
		<version>1.3.2</version>
	</dependency>
	<!-- mysql 驱动,注意确保所用驱动满足你使用的数据库版本 -->
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>8.0.13</version>
	</dependency>
	<!-- druid 驱动 -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>druid</artifactId>
		<version>1.1.12</version>
	</dependency>
</dependencies>

2、配置数据库链接属性


在application.yml文件中进行如下配置

...
mybatis:
  # 注册映射文件
  mapper-locations: classpath:com/blairscott/dao/*.xml
  # 注册实体类别名
  type-aliases-package: com.blairscott.bean

# 注册数据源,注意:不同的MySQL版本,该设置可能不同
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/db_name?useUnicode=true&characterEncoding=utf8
    username: root
    password: 123456

3、创建测试表与测试Bean


  • 创建测试表
    DROP TABLE IF EXISTS `teacher`;
    CREATE TABLE `teacher` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(16) DEFAULT NULL,
      `age` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    在这里插入图片描述
  • 创建测试实体
    package com.blairscott.bean;
    
    import lombok.Data;
    
    @Data
    public class Teacher {
        private int id;
        private String name;
        private int age;
    }
    

4、编写DAO并注册资源目录


  • 创建TeacherDao接口与映射文件

    在这里插入图片描述

    package com.blairscott.dao;
    
    import com.blairscott.bean.Teacher;
    import org.apache.ibatis.annotations.Mapper;
    
    @Mapper
    public interface TeacherDao {
        void insertTeacher(Teacher teacher);
    }
    
    <!-- TeacherDao.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.blairscott.dao.TeacherDao">
        <insert id="insertTeacher">
            INSERT INTO teacher (name, age) VALUES (#{name}, #{age})
        </insert>
    </mapper>
    
  • 注册dao包下mybatis映射文件为资源目录

    <!-- pom.xml -->
    <build>
    	...
    	<resources>
    		...
    		<!-- 注册dao包下mybatis映射文件为资源目录 -->
    		<resource>
    			<directory>src/main/java</directory>
    			<includes>
    				<include>**/*.xml</include>
    			</includes>
    		</resource>
    	</resources>
    </build>
    

5、编写测试代码


  • 服务接口与其实现类

    在这里插入图片描述

    public interface TeacherService {
        void addTeacher(Teacher teacher);
    }
    
    @Service
    public class TeacherServiceImpl implements TeacherService {
    
        @Autowired
        private TeacherDao dao;
    
        @Override
        public void addTeacher(Teacher teacher) {
            dao.insertTeacher(teacher);
        }
    }
    
  • Controller类

    在这里插入图片描述

    @Controller
    @RequestMapping("/registry")
    public class MybatisController {
    
        @Autowired
        private TeacherService service;
    
        @PostMapping("/register")
        public String register(Teacher teacher, Model model) {
            service.addTeacher(teacher);
            return "/jsp/welcome.jsp";
        }
    
    }
    
  • jsp页面

    在这里插入图片描述

    <!-- index.jsp -->
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <meta charset="UTF-8" />
        <title>Title</title>
    </head>
    <body>
        <form action="registry/register" method="post">
            姓名:<input name="name" />
            年龄:<input name="age" />
            <input type="submit" value="注册" />
        </form>
    </body>
    </html>
    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        欢迎新教师:teacher = ${teacher}
    </body>
    </html>
    

6、测试


在这里插入图片描述
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值