MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。

理解MyBatis

MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。 MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索。 MyBatis 可以使用简单的XML 或注解用于配置和原始映射,将接口和 Java 的 POJO( Plain Old Java Objects,普通的Java 对象)映射成数据库中的记录.

Mybatis的功能架构分为三层:

1)       API接口层:提供给外部使用的接口API,开发人员通过这些本地API来操纵数据库。接口层一接收到调用请求就会调用数据处理层来完成具体的数据处理。

2)       数据处理层:负责具体的SQL查找、SQL解析、SQL执行和执行结果映射处理等。它主要的目的是根据调用的请求完成一次数据库操作。

3)      基础支撑层:负责最基础的功能支撑,包括连接管理、事务管理、配置加载和缓存处理,这些都是共用的东西,将他们抽取出来作为最基础的组件。为上层的数据处理层提供最基础的支撑。

在SpringBoot中集成Mybatis

<1>在Pom中添加依赖

添加Mybatis相关Jar包

    <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.</version>
</dependency>

添加Mysql jdbc相关Jar包

  <properties>
<mysql-connector>5.1.</mysql-connector>
</properties>
    <!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector}</version>
</dependency>

<2>为项目添加配置application.properties

spring.application.name=push.messagepush01
server.port= spring.datasource.name = defaultDatasource4Phihome spring.datasource.url=jdbc:mysql://localhost:/your_database?useUnicode=true&characterEncoding=utf8&useSSL=false spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.maximum-pool-size =
spring.datasource.sql-script-encoding = UTF-
spring.datasource.min-idle =
spring.datasource.initial-size =
spring.datasource.max-active =
spring.datasource.auto-commit = true spring.datasource.validation-query=SELECT
spring.datasource.test-on-borrow=false
spring.datasource.test-while-idle=true
spring.datasource.time-between-eviction-runs-millis=

<3>编写程序代码

<3.1>编写entity:

package com.phicomm.push.messagepush01.model.entity;

public class AppDaoModel {

    private int app_id;
private String registration_id;
private String alias;
private String tag;
private String app_product_id;
private String version;
private String os_type;
private long app_regdate=0L;
private String pushcompany;
private long app_lastpushdate=0L;
private String app_wblist;
private long create_time=0L;
private long update_time=0L;
public int getApp_id() {
return app_id;
}
public void setApp_id(int app_id) {
this.app_id = app_id;
}
public String getRegistration_id() {
return registration_id;
}
public void setRegistration_id(String registration_id) {
this.registration_id = registration_id;
}
public String getAlias() {
return alias;
}
public void setAlias(String alias) {
this.alias = alias;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public String getApp_product_id() {
return app_product_id;
}
public void setApp_product_id(String app_product_id) {
this.app_product_id = app_product_id;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getOs_type() {
return os_type;
}
public void setOs_type(String os_type) {
this.os_type = os_type;
}
public long getApp_regdate() {
return app_regdate;
}
public void setApp_regdate(long app_regdate) {
this.app_regdate = app_regdate;
}
public String getPushcompany() {
return pushcompany;
}
public void setPushcompany(String pushcompany) {
this.pushcompany = pushcompany;
}
public long getApp_lastpushdate() {
return app_lastpushdate;
}
public void setApp_lastpushdate(long app_lastpushdate) {
this.app_lastpushdate = app_lastpushdate;
}
public String getApp_wblist() {
return app_wblist;
}
public void setApp_wblist(String app_wblist) {
this.app_wblist = app_wblist;
}
public long getCreate_time() {
return create_time;
}
public void setCreate_time(long create_time) {
this.create_time = create_time;
}
public long getUpdate_time() {
return update_time;
}
public void setUpdate_time(long update_time) {
this.update_time = update_time;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
private int status; }

<3.2>编写Interface Service

package com.phicomm.push.messagepush01.service;

import java.util.List;

import com.phicomm.push.messagepush01.model.entity.AppDaoModel;

public interface AppService {

    public List<AppDaoModel> getAllAppInfo();

    public List<AppDaoModel> selectAppInfo(int app_id);

    public int addAppInfo(AppDaoModel appDaoModel);

    public int updateAppInfo(AppDaoModel appDaoModel);

    public int deleteAppInfo(int app_id);
}

<3.3>编写Interface Mapper

package com.phicomm.push.messagepush01.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; import com.phicomm.push.messagepush01.model.entity.AppDaoModel; public interface AppMapper { @Select("select * from phi_push_appinfo")
public List<AppDaoModel> getAllAppInfo();
@Select("select * from phi_push_appinfo where app_id=#{app_id}")
public List<AppDaoModel> selectAppInfo(@Param("app_id") int app_id);
@Insert("insert into phi_push_appinfo(registration_id,alias,tag) values(#{appinfo.registration_id},#{appinfo.alias},#{appinfo.tag})")
public int addAppInfo(@Param("appinfo") AppDaoModel appinfo);
@Update("update phi_push_appinfo set registration_id=#{appinfo.registration_id},alias=#{appinfo.alias},tag=#{appinfo.tag} where app_id=#{appinfo.app_id}")
public int updateAppInfo(@Param("appinfo") AppDaoModel appinfo);
@Delete("delete from phi_push_appinfo where app_id=#{app_id}")
public int deleteAppInfo(@Param("app_id") int app_id);
}

<3.4>编写Service的实现

package com.phicomm.push.messagepush01.service.imp;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.phicomm.push.messagepush01.dao.AppMapper;
import com.phicomm.push.messagepush01.model.entity.AppDaoModel;
import com.phicomm.push.messagepush01.service.AppService; @Service
public class AppServiceImp implements AppService{ @Resource
private AppMapper mapper;
@Override
public List<AppDaoModel> selectAppInfo(int app_id) {
// TODO Auto-generated method stub
return mapper.selectAppInfo(app_id);
} @Override
public int addAppInfo(AppDaoModel appDaoModel) {
// TODO Auto-generated method stub
return mapper.addAppInfo(appDaoModel);
} @Override
public int updateAppInfo(AppDaoModel appDaoModel) {
// TODO Auto-generated method stub
return mapper.updateAppInfo(appDaoModel);
} @Override
public int deleteAppInfo(int app_id) {
// TODO Auto-generated method stub
return mapper.deleteAppInfo(app_id);
} @Override
public List<AppDaoModel> getAllAppInfo() {
// TODO Auto-generated method stub
return mapper.getAllAppInfo();
} }

<3.5>编写Controller

package com.phicomm.push.messagepush01.controller;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.phicomm.push.messagepush01.model.entity.AppDaoModel;
import com.phicomm.push.messagepush01.service.AppService; @RestController
@RequestMapping("/appinfo")
public class AppInfoController { private static Logger logger=LogManager.getLogger(AppInfoController.class);
@Autowired
private AppService appService; @RequestMapping("/getall")
public List<AppDaoModel> getAllAppInfo(){
logger.info("get all app info!"); List<AppDaoModel> a=appService.getAllAppInfo();
for(AppDaoModel x:a) {
logger.debug("Alias is: "+x.getAlias());
logger.debug("Tag is: "+x.getTag());
}
logger.info("select successed!");
return a;
}
@RequestMapping("/get")
public List<AppDaoModel> getAppInfo(int app_id){ return null;
}
@RequestMapping("/add")
public String regAppInfo(AppDaoModel appDaoModel) { return "";
}
@RequestMapping("/upd")
public String updAppInfo(AppDaoModel appDaoModel) { return "";
}
@RequestMapping("/del")
public String delAppInfo(int app_id) { return "";
}
}

注意项目中要单独写一个Main类用来加载Main方法

<3.6>编写入口类

package com.phicomm.push.messagepush01;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources; @SpringBootApplication(scanBasePackages= { "com.phicomm.push.messagepush01.**" })
@PropertySources(@PropertySource(value="classpath:application.properties",ignoreResourceNotFound=true))
@MapperScan("com.phicomm.push.messagepush01.dao.**")
public class PhiPushMain { public static void main(String[] args) {
SpringApplication.run(PhiPushMain.class, args);
}
}

<4>测试连接数据库

首次创建的数据库会报错,提示连不上 Access denied for user 'root'@'localhost' (using password:YES)

因为用jdbc连接数据库的时候root都不太好使,可以参考网络上的调整root的权限,也可以直接新建一个用户专门用来进行远程连接

Mybatis入门配置的更多相关文章

  1. mybatis入门配置和调试

    欢迎转载http://www.cnblogs.com/jianshuai520/p/8669177.html大家一起努力,如果看的时候有图片半边遮挡起来的话,右键查看图片,就可以观看完整的图片,具体怎 ...

  2. mybatis入门--配置

    1.导入jar包 mybatis-x.x.x.jar 导入到lib目录下, 如果使用 Maven 来构建项目,则需将下面的 dependency 代码置于 pom.xml 文件中: <depen ...

  3. Mybatis入门配置及第一个Mybatis程序

    目的:使用mybatis来进行对数据库表的操作 第一步:引入jar包 我这里是创建的maven工程 第二步:创建数据表user 第三步:创建实体类 实体类放在包 com.xxx.pojo 下,包名可自 ...

  4. MyBatis入门程序(基于XML配置)

    创建一个简单的MyBatis入门程序,实现对学生信息的增删改查功能(基于XML配置) 一.新建一个Java工程,导入MyBatis核心jar包.日志相关的jar包以及连接Oracle数据库所需驱动包, ...

  5. Mybatis入门(四)配置别名(二)

    这一章我们练习一下Mybatis的别名,这大大的提高了我们的开发效率 类型别名(typeAliases) 类型别名是为 Java 类型设置一个短的名字. 它只和 XML 配置有关,作用在于用来减少类完 ...

  6. Mybatis入门(四)配置优化(一)

    这一章主要实验Mybatis的引入外部配置文件,属性(properties)这个属性都是可外部配置且可动态替换的,既可以在典型的 Java 属性文件中配置,亦可通过 properties 元素的子元素 ...

  7. MyBatis1:MyBatis入门

    MyBatis是什么 MyBatis是什么,MyBatis的jar包中有它的官方文档,文档是这么描述MyBatis的: MyBatis is a first class persistence fra ...

  8. MyBatis入门基础(一)

    一:对原生态JDBC问题的总结 新项目要使用mybatis作为持久层框架,由于本人之前一直使用的Hibernate,对mybatis的用法实在欠缺,最近几天计划把mybatis学习一哈,特将学习笔记记 ...

  9. MyBatis入门案例、增删改查

    一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...

随机推荐

  1. loadimage1();有问题

    f.Read(pBuffer, nSize)不能少,少了虽然能读进去数据但是不能显示成图片,可能原因是存进的数据并不是图片数据! 输入图片测试,但是没有CFile先Open再Read

  2. android Contacts/Acore进程常常被Kill,导致联系人开机后丢失怎么办?

    Contacts/Acore进程,在内存较少和开机进程过多的情况下会常常被 ActivityManager Kill 掉. 导致Sim卡联系人开机后未导入或者仅仅导入一部分,造成联系人丢失的现象,可是 ...

  3. iOS开发之--搭建本地的SVN服务器

    近期入职的新公司,后台没有分配svn账号,需要在本地搭建一个服务器,方便和代码,看了看网上的教程,一直有这样那样的问题, 其中最主要的问题还是路径拼接的问题,最后终于解决了,特在此分享下,如果大家有更 ...

  4. SqlSession接口和Executor

    mybatis框架在操作数据的时候,离不开SqlSession接口实例类的作用.可以说SqlSession接口实例是开发过程中打交道最多的一个类.即是DefaultSqlSession类.如果笔者记得 ...

  5. Mybatis整理系列(01)————传入参数方式以及#{}与${}的区别

    一.在MyBatis的select.insert.update.delete这些元素中都提到了parameterType这个属性.MyBatis现在可以使用的parameterType有基本数据类型和 ...

  6. Objective-C代码学习大纲(2)

    2011-05-11 14:06 佚名 otierney 字号:T | T 本文为台湾出版的<Objective-C学习大纲>的翻译文档,系统介绍了Objective-C代码,很多名词为台 ...

  7. ie8兼容:对象不支持“trim”属性或方法

    trim() 方法是原生js中去空格的方法,高版本浏览器已经默认支持trim() 方法,但ie8以下不支持,会报错:对象不支持“trim”属性或方法 解决这个的兼容,只需要扩展String原型属性 在 ...

  8. js 匿名函数-立即调用的函数表达式

    先提个问题, 单独写匿名函数为什么报错?return 匿名函数 为什么不报错? 如图: 第二种情况在 f 还没有执行的时候,就报错了,,,当然这得归因于函数声明语句声明提前(发生在代码执行之前)的原因 ...

  9. Maven聚合、Maven仓库jar包以及Spring+MyBatis+JUnit+Maven整合测试的搭建过程

    一.Maven将父项目创建到父项目的内部 在父项目的pom.xml上 点右键,选择maven-->new-->maven module  project 二.Maven聚合 在某个项目的p ...

  10. Ubuntu 14.04 安装jdk,tomcat

     分类: 碎知识(8)  版权声明:本文为博主原创文章,未经博主允许不得转载. 写在前面: 装的时候,参考了许多网上的资料,有很多人写的有些简单了,人家那边版本稍微一更新,像我这样的小白就找不到东南西 ...