MyBatis框架

MyBatis框架

​ Mybatis,spring,springMVC三大框架

​ Hibernate,spring,strus三大框架

Mybatis 和hibernate的区别

Hibernate是全自动的,mybatis是半自动的。Hibernate能够自动生成sql
Hibernate数据库移植性大于mybatis。
Hibernate拥有完整日志系统:sql记录,关系异常,优化警告,缓存提示。
Myabtis更加关心细节,简单易学,开发模式上与传统的jdbc区别小。
Mybatis在sql有优化上,比hibernate方便。

Mybatis编程步骤

  1. 通过mybatis-config配置文件创建sqlSessionFactory

  2. 通过sqlSessionFactory创建sqlSession

  3. 通过sqlSession执行数据库操作

  4. Session.commit提交事务

  5. Session.close关闭

jar包

在这里插入图片描述

Log4j配置

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.ruanyuan=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

Mybatis配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    
<configuration>
		<!-- 配置默认环境,environments可配置多个数据库环境! -->
	<environments default="mysql">
		<!-- 配置mysql数据库环境 -->
		<environment id="mysql">
			<!-- 使用jdbc的事务管理 -->
			<transactionManager type="JDBC"></transactionManager>
			<!-- 数据库连接池 -->
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/systop_crm?useUnicode=true&amp;characterEncoding=UTF8"/>
				<property name="username" value="root"/>
				<property name="password" value=""/>
			</dataSource>
		</environment>
	</environments>
	
	<!-- 配置映射文件  -->
	<mappers>
		<mapper resource="com/ruanyuan/dao/CustomerMapper.xml"/>
		<mapper resource="com/ruanyuan/dao/UserMapper.xml"/>
	</mappers>
</configuration>

Mapper映射文件

<?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.ruanyuan.dao.CustomerMapper">
	<select id="findCustomerById" resultType="com.ruanyuan.pojo.Customer">
		select * from customer where cust_id = #{cust_id};
	</select>
	<select id="findCustomerByLikeName"  resultType="com.ruanyuan.pojo.Customer">
		select * from customer where cust_name like "%" #{cust_name} "%";
	</select>
	<select id="findRowByUserId" resultType="com.ruanyuan.pojo.Customer">
		select * from customer 
		where cust_user_id = #{cust_user_id} 
		or cust_create_id = #{cust_create_id};
	</select>	


<?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.ruanyuan.dao.CustomerMapper">
	<select id="findCustomerById" resultType="com.ruanyuan.pojo.Customer">
		select * from customer where cust_id = #{cust_id};
	</select>
	<select id="findCustomerByLikeName"  resultType="com.ruanyuan.pojo.Customer">
		select * from customer where cust_name like "%" #{cust_name} "%";
	</select>
	<select id="findRowByUserId" resultType="com.ruanyuan.pojo.Customer">
		select * from customer 
		where cust_user_id = #{cust_user_id} 
		or cust_create_id = #{cust_create_id};
	</select>
</mapper>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Java123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值