从JDBC到Mybatis

一、从JDBC到Mybatis的改进

1.JDBC

      Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。 

2. mybatis

      MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。
iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAOs)。

JDBC存在的问题:

  1. 代码比较多,开发效率低。
  2. 需要关注 Connection ,Statement, ResultSet 对象创建和销毁。
  3. 对 ResultSet 查询的结果,需要自己封装为 List。 4. 重复的代码比较多。
  4. 业务代码和数据库的操作混在一起,不利于现代的开发习惯。

mybatis解决的问题:

  1. 减轻使用 JDBC 的复杂性,不用编写重复的创建 Connetion , Statement 。
  2. 不用编写关闭资源代码。
  3. 直接使用 java 对象,表示结果数据。让开发者专注 SQL 的处理。 其他分心的工作由 MyBatis 代劳。

 二、IDEA环境下Mybatis对JDBC进行改造示例对比

1.新建项目

 

 

 

2.项目配置

 修改application.properties文件 

server.port=8080
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false 
spring.datasource.username=账户 
spring.datasource.password=密码
mybatis.mapper-locations=classpath:mapper/*Mapper.xml 

3.建立项目文件

UserController.Java:

package com.example.test.controller;

import com.example.test.entity.User;
import com.example.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getAllUser")
    public List<User> findAll(){
        return userService.findAllUser();
    }

    @RequestMapping("/getUserByUserID/{userid}")
    public List<User> findUserByUserId(@PathVariable int userid){
        return userService.findUserByUserId(userid);
    }
}


 

User.Java

package com.example.test.entity;

public class User {
    private int userid;
    private String username;
    private String password;

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "userid=" + userid +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

 

UserMapper.Java

package com.example.test.mapper;

import com.example.test.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {
    public List<User> findAllUser();
    public List<User> findUserByUserId(int userid);
}

 UserService.Java

 

package com.example.test.service;

import com.example.test.entity.User;
import com.example.test.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired(required=false)
    public UserMapper userMapper;

    public List<User> findAllUser(){
        return userMapper.findAllUser();
    }

    public List<User> findUserByUserId(int userid){
        return userMapper.findUserByUserId(userid);
    }
}

4.建立mapper文件

 

UserMapper.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.test.mapper.UserMapper">
    <resultMap id="result" type="com.first.mybatis_example.entity.User">
        <result column="id" jdbcType="INTEGER" property="userid" />
        <result column="name" jdbcType="VARCHAR" property="username" />
        <result column="password" jdbcType="VARCHAR" property="password" />
    </resultMap>

    <select id="findAllUser" resultType="com.first.mybatis_example.entity.User">
        select  * from user;
    </select>

    <select id="findUserByUserId" resultType="com.first.mybatis_example.entity.User">
        select * from user where userid=#{userid};
    </select>
</mapper>

 5.MySQL创建表格user

create table user(
	id int primary key auto_increment,
	name varchar(50),
	password varchar(50)


)character set utf8 collate utf8_general_ci;

 

 6.查询 http://localhost:8080/user/getAllUser

 参考:

(20条消息) IDEA使用mybatis连接MySQL_junseven164的博客-CSDN博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值