20、(知识篇)Mybatis使用

/**

* MyBatis使用方法

* 1、到官网下载相关包 http://www.mybatis.org/mybatis-3/

* 2、创建xml的配置文件,详细配置可以在官网找到,直接copy过来用就好了(建议引入dtd

*  2.1 :需要配置mapper,即是写sql的那个文件,写上对应的路径即可

*      例如:<mapper resource="com/mybatis/vo/userMapper.xml"/>

*  2.2:配置别名,可以再映射文件中,直接用别名,就不用写全类名了

*    例子:<typeAliases><typeAlias type="com.mybatis.vo.User" alias="User"/></typeAliases>

*  2.3:可以通过 properties引入外部文件 然后用${xxx}的方式写到xml中去

* 3、Mapper的使用

*  3.0 格式可以在官网找到,直接copy过来用就好了(建议引入dtd

*  3.1 命名空间: <mapper namespace="com.mybatis.vo.User">

*  3.2 增删该查标签:insert/update/delete/select 然后再标签里面写上对应语句

*  3.3parameterType/resultType 需要写上全类名,或者可以用2.2的方式使用别名

*  3.4占位符#{xxxx} 如果是对象,则xxx为对象的属性名

* 4、获取SqlSessionFactory/SqlSession

*  4.1通过类加载器获取xml的配置文件

*  4.2 通过 new SqlSessionFactoryBuilder().build(is)获得sqlsessionfactory

*  4.3 通过 sessionFactory.openSession() 获得session

*  4.4调用mapper

* mybatis通过session调用 insert(xxxx)/update(xxxx)/delete(xxxx)/selectOne(xxx)/selectList(xxx)执行方法

* 其中xxx 需要填写3.1的 命名空间和 调用标签的id  格式  命名空间.标签id

* 5、mybatis默认是手动commit,所以执行完sql之后,需要手动commit/close

*     可以在session.openSession(true);设置自动提交

* 6、额外知识(二级缓存)这个只做了下了解,日后需要再深入研究吧

* 注意使用二级缓存的mapper的对象需要实现序列化

*  只需要再mapper文件中加入  <cache />标签即可

*  这样即使用两个 session去查询(必须分别提交事务), 当数据没有更新时做查询,则不会再次进行物理查询(即不会到数据库执行两次)

* 一般使用在那些数据不频繁更新的表 或者对数据准确性没有那么严格要求的情况中

* @param args

*/


测试类:

package com.mybatis.test;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.mybatis.vo.User;

public class Test {
	
	/**
	 * MyBatis使用方法
	 * 1、到官网下载相关包 http://www.mybatis.org/mybatis-3/
	 * 2、创建xml的配置文件,详细配置可以在官网找到,直接copy过来用就好了(建议引入dtd)
	 * 	 2.1 :需要配置mapper,即是写sql的那个文件,写上对应的路径即可
	 * 		     例如:<mapper resource="com/mybatis/vo/userMapper.xml"/>
	 * 	 2.2:配置别名,可以再映射文件中,直接用别名,就不用写全类名了
	 * 		   例子:<typeAliases><typeAlias type="com.mybatis.vo.User" alias="User"/></typeAliases>
	 * 	 2.3:可以通过 properties引入外部文件 然后用${xxx}的方式写到xml中去
	 * 3、Mapper的使用
	 * 	 3.0 格式可以在官网找到,直接copy过来用就好了(建议引入dtd)
	 * 	 3.1 命名空间: <mapper namespace="com.mybatis.vo.User">
	 * 	 3.2 增删该查标签:insert/update/delete/select 然后再标签里面写上对应语句
	 * 	 3.3parameterType/resultType 需要写上全类名,或者可以用2.2的方式使用别名
	 * 	 3.4占位符#{xxxx} 如果是对象,则xxx为对象的属性名
	 * 4、获取SqlSessionFactory/SqlSession
	 * 	 4.1通过类加载器获取xml的配置文件
	 * 	 4.2 通过 new SqlSessionFactoryBuilder().build(is)获得sqlsessionfactory
	 * 	 4.3 通过 sessionFactory.openSession() 获得session
	 * 	 4.4调用mapper
	 * 		mybatis通过session调用 insert(xxxx)/update(xxxx)/delete(xxxx)/selectOne(xxx)/selectList(xxx)执行方法
	 * 		其中xxx 需要填写3.1的 命名空间和 调用标签的id  格式  命名空间.标签id
	 * 5、mybatis默认是手动commit,所以执行完sql之后,需要手动commit/close
	 * 	    可以在session.openSession(true);设置自动提交
	 * 
	 * 6、额外知识(二级缓存)这个只做了下了解,日后需要再深入研究吧
	 * 	注意使用二级缓存的mapper的对象需要实现序列化
	 * 	 只需要再mapper文件中加入  <cache />标签即可
	 * 	 这样即使用两个 session去查询(必须分别提交事务), 当数据没有更新时做查询,则不会再次进行物理查询(即不会到数据库执行两次)
	 * 	一般使用在那些数据不频繁更新的表 或者对数据准确性没有那么严格要求的情况中
	 * 
	 * @param args
	 */
	
	public static void main(String[] args) {
		
		//通过类加载器获取xml的配置文件
		InputStream is = Test.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
		
		System.out.println(is);
		
		SqlSessionFactory sessionFactory =  new SqlSessionFactoryBuilder().build(is);
		
		System.out.println(sessionFactory);
		
		SqlSession session = sessionFactory.openSession();
		
		System.out.println(session);
		
		
		String nameSpace = "com.mybatis.vo.User";
		
		String addValue = nameSpace+".addUser";
		/*int row = 0;
		row = session.insert(addValue,new User(0, "Tester2", 1));
		System.out.println(row);
		
		String updateValue = nameSpace+".updateUser";
		row = session.update(updateValue,new User(1, "Tester1", 1));
		System.out.println(row);
		*/
		
		
		String selectValue = nameSpace+".selectUser";
		User user = session.selectOne(selectValue,1);
		System.out.println(user);
		
		String selectAllValue = nameSpace+".selectAllUser";
		List<User> users = session.selectList(selectAllValue);
		System.out.println(users);
		
		String selectCountValue = nameSpace+".selectCount";
		long count = session.selectOne(selectCountValue);
		System.out.println(count);
		
		
		session.commit();
		session.close();
	}
}


实体类:
package com.mybatis.vo;

public class User {
	private int id;
	private String userName;
	private int sex;
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(int id, String userName, int sex) {
		super();
		this.id = id;
		this.userName = userName;
		this.sex = sex;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public int getSex() {
		return sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", userName=" + userName + ", sex=" + sex + "]";
	}
	
}

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.mybatis.vo.User">

   <!-- 二级缓存 <cache /> -->
  <insert id="addUser" parameterType="User">
  	insert into `User` (userName,sex) values(#{userName},#{sex})
  </insert>
  <update id="updateUser" parameterType="User">
  	update User set userName = #{userName},sex=#{sex} where id = #{id}
  </update>
  
  <select id="selectAllUser" resultType="User">
  	select * from User
  </select>
  
  <select id="selectCount" resultType="java.lang.Long">
  	select count(1) from User
  </select>
  
  <select id="selectUser" resultType="User">
    select * from User where id = #{id}
  </select>
</mapper>

mybatis-config.xml

<?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="db.properties"></properties> -->
	
	<!-- 别名的使用 -->
	<typeAliases>
		<typeAlias type="com.mybatis.vo.User" alias="User"/>
	</typeAliases>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://192.168.1.92:3306/test" />
				<property name="username" value="root" />
				<property name="password" value="root" />
			</dataSource>
		</environment>
	</environments>
	<!-- 映射文件 -->
	<mappers>
		<!-- <mapper resource="org/mybatis/example/BlogMapper.xml" /> -->
		<mapper resource="com/mybatis/vo/userMapper.xml"/>
	</mappers>
</configuration>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值