MyBatis框架

什么是MyBatis?

MyBatis是对JDBC的封装,它隐藏了具体的JDBC的API,它把SQL语句放到了配置文件中,它能自动把不同的输入数据映射到SQL语句的动态参数上,它能自动把SQL语句的执行结果映射为JAVA对象…… MyBatis是一个持久化层的框架!

1.先导入jar包

2.编写排至文件,在类路径下

配置文件(SqlMapConfig.xml)

基本作用就是配置JDBC连接的有关信息,比如URL、用户名、密码等等

如:

<?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>
	<typeAliases>
		<typeAlias type="cn.com.leadfar.cms.backend.model.Admin" alias="Admin"/>
	</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://localhost/cms" />
				<property name="username" value="root" />
				<property name="password" value="mysql" />
			</dataSource>
		</environment> 
	</environments>
	<mappers>
		<mapper resource="cn/com/leadfar/cms/backend/dao/impl/AdminMapper.xml" />
	</mappers>
</configuration>

一般情况下配置的路径时写一些和数据库中属性名相同的一些参数,如:

3.写SQL语句

<?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="aa.bb.cc">
<!-- 配置对象的属性列表中的字段的映射 -->
 
<resultMap type="Article" id="articleMap">
<result property="asource" column="asource"/><!-- 类里面定义的变量,数据库里的属性变量  -->
</resultMap>

	<insert id="add" parameterType="Article">
		insert into t_article (title,asource,keyword,content) values (#{title},#{asource},#{keyword},#{content})
	</insert>
	<delete id="del" parameterType="int">
	   delete from t_article where id=#{a}
	</delete>
	<update id="update" parameterType="Article">
	  update t_article set title=#{title},asource=#{asource},keyword=#{keyword},content=#{content} where id=#{id}
	</update>
	<select id="list-count" resultType="int">
     select count(*) from t_article
	</select>
	<select id="list" parameterType="map" resultMap="articleMap">
	select * from t_article limit #{offset},#{pagesize}
	</select>
	<select id="findById" parameterType="int" resultMap="articleMap">
	   select * from t_article where id=#{b}
	</select>
</mapper>

resultMap是调用上面的配置,parameterType是不调用配置

public class DBUtil {
  public static SqlSessionFactory factory = null;
  static{
	
			try {
				SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
				Reader r = Resources.getResourceAsReader("SqlMapConfig.xml");
				factory = builder.build(r);//创建出工厂
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
  }
  public static SqlSession getSession(){
	  return factory.openSession();
  }
}
public class TestMyBites {
 
	@Test
	public void test1(){
		SqlSession session = null;
		try {
			
		session = DBUtil.getSession();
			Article a = new Article();
			a.setTitle("框架封装");
			a.setAsource("数据库");
			a.setKeyword("数据库框架");
			a.setContent("数据库框架的配置封装");
			session.insert("aa.bb.cc.add", a);
			//一定要提交,不然数据库不显示
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.rollback();
		}finally{
			if(session!=null)
				session.close();
		}
	}
	@Test
	public void test2(){
		SqlSession session = null;
		try{
		session = DBUtil.getSession();
		session.delete("aa.bb.cc.del", 78);
		session.commit();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		session.rollback();
	}finally{
		if(session!=null)
			session.close();
	}
	}
	@Test
	public void test3(){
		SqlSession session = null;
		try{
		session = DBUtil.getSession();
		Article a = (Article) session.selectOne("aa.bb.cc.findById", 75);
		System.out.println(a.getId()+" "+a.getContent());
		session.commit();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		session.rollback();
	}finally{
		if(session!=null)
			session.close();
	}	
	}
	@Test
	public void test4(){
		SqlSession session = null;
		try{
		session = DBUtil.getSession();
		int count = (Integer)session.selectOne("aa.bb.cc.list-count");
		Map<String, Integer> m = new HashMap<String, Integer>();
		m.put("offset",0);
		m.put("pagesize", 10);
		List<Article> a = (List<Article>)session.selectList("aa.bb.cc.list",m );
		
		Iterator<Article> it = a.iterator();
		while(it.hasNext()){
			Article aa = it.next();
			System.out.println(aa.getAsource()+" "+aa.getContent()+" "+aa.getId());
		}
		session.commit();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		session.rollback();
	}finally{
		if(session!=null)
			session.close();
	}	
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值