Mybatis学习笔记

MyBatis

导入需要的Jar 

配置config.xml

一般放在SRC 目录下即可,XML配置文件包含对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>

<!--外部的配置文件  -->

<properties resource="jdbc.properties"></properties>

<!-- 类型别称 -->

<typeAliases>

<typeAlias type="com.ytc.model.News" alias="News"/>

<typeAlias type="com.ytc.model.User" alias="User"/>

</typeAliases>

<!--核心的环境配置 default:默认应用的环境名  -->

<environments default="oracle">

<!-- id: 环境名称 -->

<environment id="oracle">

<!--事务处理:常用JDBC-->

<transactionManager type="JDBC"></transactionManager>

<!-- 数据连接池 -->

<dataSource type="pooled">

<property name="driver" value="${driver}"/>

<property name="url" value="${url}"/>

<property name="username" value="${username}"/>

<property name="password" value="${password}"/>

</dataSource>

</environment>

</environments>

<!-- Mapper对应相应Java类的XML配置文件 -->

<mappers>

<mapper resource="com/ytc/model/News.xml"/>

</mappers>

</configuration>

上例中的jdbc.properties 文件同样位于SRC目录下,内容如下

driver=oracle.jdbc.driver.OracleDriver

url=jdbc:oracle:thin:@localhost:1521:XE

username=scott

password=tiger

配置SQL 语句映射的XML

上例中的<mapper resource="com/ytc/model/News.xml"/> News.xml即为SQL的映射文件

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.ytc.model.news">

<!-- 查询语句,id:调用时的名称 resultType:返回类型,可以使用别名 -->

<select id="getAllNews" resultType="News">

select * from news

</select>

<!-- 带参数的查询语句 -->

<select id="getNews" resultType="News" parameterType="Page"> 

<!-- 其中CDATA申明内的语句不进行xml解析

#{xxx} 会自动调用对象的getter方法-->

<![CDATA[

select t.* from 

(select Rownum rn , n.* from news n where rownum <=#{end}) t

where t.rn>#{begin}

]]>

</select>

</mapper>

Java中获得SQLSession对象

public class SessionFactory {

private static SqlSessionFactory factory;

static {

Reader reader;

try {

reader = Resources.getResourceAsReader("config.xml");

factory = new SqlSessionFactoryBuilder().build(reader);

catch (IOException e) {

e.printStackTrace();

}

}

public SqlSession getSession(){

return factory.openSession();

}

public static void main(String[] args) {

SqlSession s = new SessionFactory().getSession();

System.out.println(s);

}

}

一、调用方式

可以将Mapper映射的xml 中的方法,写成一个Interface

动态SQL语句

  html连接:动态SQL 

模糊查询:

<select id="queryNews" resultType="News" parameterType="News" >

 select n.* from news n 

 <where>

  <if test="title != null and title != '' ">

  title Like '%${title}%'

  </if>

  <if test="content != null and content != '' ">

  and content like '%${content}%'

  </if>

 </where>

</select>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值