springboot学习系列二:springboot集成mybatis

目录

 

pom.xml文件

application.yml配置文件

Mapper、实体文件


pom.xml文件

Springboot项目集成mybatis时,需要引入mybatis相应的依赖。

<dependency>

    <groupId>org.mybatis.spring.boot</groupId>

    <artifactId>mybatis-spring-boot-starter</artifactId>

    <version>2.1.1</version>

</dependency>

application.yml配置文件

server:
   port:  8090
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
    username: root
    password: 123456
#指定mybatis映射文件的地址     mapper-locations为mapper资源文件的路径,type-aliases-package为dao层接口文件存放的目录
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.demo.mapper

Mapper、实体文件

(1)利用eclipse的“mybatis generator”生成mapper、实体文件,参照https://blog.csdn.net/mudisheng0202/article/details/106987755

(2)数据库表及对应实体

    1)数据库表

CREATE TABLE `ay_user` (
  `id` varchar(32) NOT NULL COMMENT '主键',
  `name` varchar(10) DEFAULT NULL COMMENT '用户名',
  `password` varchar(32) DEFAULT NULL COMMENT '密码',
  `mail` varchar(32) DEFAULT NULL COMMENT '邮箱',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of ay_user
-- ----------------------------
INSERT INTO `ay_user` VALUES ('1', '张三', '654321', '1234567890@qq.com');
INSERT INTO `ay_user` VALUES ('2', '李四', '123456', '9876543210@qq.com');
INSERT INTO `ay_user` VALUES ('3', '王麻子', '654321', '5432167890@qq.com');

    2)实体类

package com.example.demo.entity;

public class AyUser {
    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column ay_user.id
     *
     * @mbg.generated
     */
    private String id;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column ay_user.name
     *
     * @mbg.generated
     */
    private String name;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column ay_user.password
     *
     * @mbg.generated
     */
    private String password;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column ay_user.mail
     *
     * @mbg.generated
     */
    private String mail;

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column ay_user.id
     *
     * @return the value of ay_user.id
     *
     * @mbg.generated
     */
    public String getId() {
        return id;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column ay_user.id
     *
     * @param id the value for ay_user.id
     *
     * @mbg.generated
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column ay_user.name
     *
     * @return the value of ay_user.name
     *
     * @mbg.generated
     */
    public String getName() {
        return name;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column ay_user.name
     *
     * @param name the value for ay_user.name
     *
     * @mbg.generated
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column ay_user.password
     *
     * @return the value of ay_user.password
     *
     * @mbg.generated
     */
    public String getPassword() {
        return password;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column ay_user.password
     *
     * @param password the value for ay_user.password
     *
     * @mbg.generated
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column ay_user.mail
     *
     * @return the value of ay_user.mail
     *
     * @mbg.generated
     */
    public String getMail() {
        return mail;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column ay_user.mail
     *
     * @param mail the value for ay_user.mail
     *
     * @mbg.generated
     */
    public void setMail(String mail) {
        this.mail = mail;
    }
}

(3)mapper接口、mapper.xml文件

    1)mapper接口

package com.example.demo.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import com.example.demo.entity.AyUser;

@Mapper
public interface AyUserMapper {
	/**
	 * 
	 * @param name
	 * @param password
	 * @return
	 */
	AyUser getAyUserByNameAndPassword(@Param("name") String name,@Param("password") String password);
}

    2)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.example.demo.mapper.AyUserMapper">
  <resultMap id="BaseResultMap" type="com.example.demo.entity.AyUser">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="mail" jdbcType="VARCHAR" property="mail" />
  </resultMap>
  <sql id="Base_Column_List">
    id, name, password, mail
  </sql>
  <select id="getAyUserByNameAndPassword" parameterType="java.lang.String" resultMap="BaseResultMap">
  	select 
  	<include refid="Base_Column_List" />
  	from ay_user user
  	<where>
  		user.name = #{name}
  		and
  		user.password = #{password}
  	</where>	
  </select>
</mapper>

(4)service层

    1)service层接口

package com.example.demo.service;

import com.example.demo.entity.AyUser;

public interface AyUserService {
	
	
	AyUser getAyUserByNameAndPassword(String name,String password);

}

    2)service接口实现类

package com.example.demo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.entity.AyUser;
import com.example.demo.mapper.AyUserMapper;
import com.example.demo.service.AyUserService;

@Service()
public class AyUserServiceImpl implements AyUserService {
	
	@Autowired
	private AyUserMapper ayUserMapper;

	@Override
	public AyUser getAyUserByNameAndPassword(String name, String password) {
		// TODO Auto-generated method stub
		return ayUserMapper.getAyUserByNameAndPassword(name, password);
	}

}

(5)测试类

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.demo.entity.AyUser;
import com.example.demo.service.AyUserService;

@SpringBootTest
class SpringmybatisdemoApplicationTests {

	@Test
	void contextLoads() {
	}
	
	@Autowired
	private AyUserService ayUserService;
	
	@Test
	public void testSpringbootAndMybatis() {
		
		AyUser ayUser = ayUserService.getAyUserByNameAndPassword("张三","654321");
		
		System.out.println(ayUser.getId()+","+ayUser.getName()+","+ayUser.getMail());
		
	}

}

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值