分分钟让你认识MyBatis

        MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。具体概念,想要跟多了解可以去问度娘。此处不多言。

 

最近在开发屋祺的项目,使用的持久层框架是MyBatis,使用XML文件直接映射数据库中的记录,特别方便。

 

既然是使用xml文件直接映射数据库中的记录,那么我们要先将数据库创建好。下面先简单的创建一个数据库,里边有一张usrs.(我使用的是mysql数据库。)

 

 1、创建数据库,这个大家都会,就不多说了。


 

表如下图:



2、写xml文件。


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >

<generatorConfiguration>
	<classPathEntry location="D:/mysql-connector-java-5.1.7.jar" />
	<context id="MySqlTables" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8"
			userId="root" password="root">
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<javaModelGenerator targetPackage="mybatis.model"
			targetProject="test\src">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<sqlMapGenerator targetPackage="mybatis.mapping"
			targetProject="test\src">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="mybatis.dao" targetProject="test\src">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<table tableName="users">
			<generatedKey column="id" sqlStatement="MySql" identity="true" />
		</table>
	</context>
</generatorConfiguration>


3、执行Generate MyBatis/iBATIS Artifacts



4、生成相应的dao.mappingmodel




Dao层:

package mybatis.dao;

import java.util.List;
import mybatis.model.Users;
import mybatis.model.UsersExample;
import org.apache.ibatis.annotations.Param;

public interface UsersMapper {
    int countByExample(UsersExample example);

    int deleteByExample(UsersExample example);

    int deleteByPrimaryKey(Integer id);

    int insert(Users record);

    int insertSelective(Users record);

    List<Users> selectByExample(UsersExample example);

    Users selectByPrimaryKey(Integer id);

    int updateByExampleSelective(@Param("record") Users record, @Param("example") UsersExample example);

    int updateByExample(@Param("record") Users record, @Param("example") UsersExample example);

    int updateByPrimaryKeySelective(Users record);

    int updateByPrimaryKey(Users record);
}

Maping相应的sql语句,都在这里,可以在他的基础上修改,修改成自己要的sql语句。

Model层:

package mybatis.model;

public class Users {
    private Integer id;

    private String name;

    private String password;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }
}

使用mybatis的优点:

 

1、易于上手和掌握

2SQL写字XML文件里,便于统一管理和优化。

3、接触SQL与程序代码的耦合

4、提供映射标签,支持对象与数据库的ORM字段关系映射。


       HibernateMybatis都是比较优秀的O/Rmapping框架,Hibernate对于数据库结构提供了比较完整的封装,HibernateO/R Mapping实现了POJO和数据库表之间的映射,以及SQL的自动生成和执行。

       MyBatis则是通过映射配置文件就是xml文件,将SQL所需的参数,以及返回的结构字段映射到指定的POJO



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值