mybatis入门基础操作数据库

//封装一个获取Session的类方法


package cn.bdqn.utils;


import java.io.IOException;
import java.io.InputStream;


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


public class SessionUtils {
private static SqlSessionFactory factory;
static {
InputStream is;
try {
is = Resources.getResourceAsStream("mybatis-config.xml");
factory = new SqlSessionFactoryBuilder().build(is);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}

public static SqlSession getSession(){
return factory.openSession(false);


}
public static void close(SqlSession session){
if (session!=null) {
session.close();
}

}
}

//配置dao层的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="cn.bdqn.dao.UserDao">
<select id="count" resultType="int">
select count(1) from student
</select>
<select id="selectAll" resultType="cn.bdqn.entity.Student">
select * from student
</select>
<delete id="deleteById" parameterType="int">


delete from student where
uid=#{uid}
</delete>
<update id="updateById" parameterType="cn.bdqn.entity.Student">
update student set uname=#{uname},upwd=#{upwd} where uid=#{uid}
</update>
<select id="selectByName" parameterType="String" resultType="cn.bdqn.entity.Student">
select *from student where uname like concat ('%',#{name},'%')
</select>
<insert id="addStu" parameterType="cn.bdqn.entity.Student" >

insert into student values(#{uid},#{uname},#{upwd})
</insert>


</mapper>


//创建一个dao层

package cn.bdqn.dao;


import java.util.List;


import cn.bdqn.entity.Student;


/**
 * @author Administrator
 *
 */
/**
 * @author Administrator
 *${date}
 *
 * @date:2018-1-11 下午4:20:57
 *
 *UserDao.javacn.bdqn.dao下午4:20:57UserDaoAdministrator
 */
/**
 * @author Administrator
 *${date}
 *
 * @date:2018-1-11 下午4:21:25
 *
 *UserDao.javacn.bdqn.dao下午4:21:25UserDaoAdministrator
 */
public interface UserDao {
/**
* @return 
*/
List<Student> selectAll();
/**
* @param name
* @return
*/
List<Student> selectByName(String name);
int deleteById(int uid);
int updateById(Student student);
int addStu(Student student);
}


package cn.bdqn.test;



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


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


import cn.bdqn.dao.UserDao;
import cn.bdqn.entity.Student;
import cn.bdqn.utils.SessionUtils;


/**
 * @author Administrator ${date}
 * 
 * @date:2018-1-11 下午4:19:41
 * 
 *                 Test.javacn.bdqn.test下午4:19:41TestAdministrator
 */
public class Test {


public static void main(String[] args) {
/*
* InputStream is =null; try {
* is=Resources.getResourceAsStream("mybatis-config.xml");
* SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
* SqlSession session =factory.openSession(); int count =
* session.selectOne("cn.bdqn.dao.UserMapper.count");
* System.out.println(count); } catch (IOException e) { // TODO
* Auto-generated catch block e.printStackTrace(); } finally{ try {
* is.close(); } catch (IOException e) { // TODO Auto-generated catch
* block e.printStackTrace(); }

* }
*/
/*
* SqlSession session = null; try { session = SessionUtils.getSession();
* int count =session.getMapper(UserDao.class).deleteById(1);
* session.commit(); if (count>0) { System.out.println("删除成功"); }else {
* System.out.println("失败"); } } catch (Exception e) { // TODO
* Auto-generated catch block e.printStackTrace(); session.rollback(); }
*/
/* List<Student> students=session.getMapper(UserDao.class).selectAll(); */
/*
* for (Student student : students) {
* System.out.println(student.toString()); }
*/
/* System.out.println(count); */


/*
* SqlSession session = null; try { Student student= new Student();
* student.setUid(2); student.setUname("qqqqq");
* student.setUpwd("ccccc");

* session = SessionUtils.getSession(); int count
* =session.getMapper(UserDao.class).updateById(student);
* session.commit(); if (count>0) { System.out.println("删除成功"); }else {
* System.out.println("失败"); } } catch (Exception e) { // TODO
* Auto-generated catch block e.printStackTrace(); session.rollback(); }
* SessionUtils.close(session);
*/
/*
* SqlSession session = null; try {


* session = SessionUtils.getSession(); List<Student>
* students=session.getMapper(UserDao.class).selectByName("q"); for
* (Student student : students) {
* System.out.println(student.toString()); }

* } catch (Exception e) { // TODO Auto-generated catch block
* e.printStackTrace(); session.rollback(); }
* SessionUtils.close(session);
*/
SqlSession session = null;
try {


Student student = new Student();
student.setUid(4);
student.setUname("gh676h6");
student.setUpwd("ccch46h4cc");
session = SessionUtils.getSession();
int count = session.getMapper(UserDao.class).addStu(student);
System.out.println(count);


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
session.rollback();
}
SessionUtils.close(session);


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值