咖啡里的糖的gravatar头像
咖啡里的糖 2017-01-12 03:26:48

mybatis基础学习,通过main函数测试数据库读写

第一次玩这个不会发!见谅!注释写的不多!不过附带了一个mybatis的总结!因为懒就没有写成web是一个单纯的java项目!里面有mybatis的基于接口,基于映射,基于注解的写法都有,一对一,一对多,动态查询...貌似就这一个页面写了注释!慢慢看能看懂

package studymapper.dao;

import java.io.IOException;
import java.io.Reader;
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 org.apache.log4j.PropertyConfigurator;

import studymapper.bean.CardBean;
import studymapper.bean.LazystudentBean;
import studymapper.bean.ScoreBean;
import studymapper.bean.StudentBean;
import studymapper.bean.StudentBean1;
import studymapper.bean.StudentBean2;
import studymapper.bean.StudentBean3;

public class Mapper {
	private CardBean card;
	private LazystudentBean lazy;
	private ScoreBean score;
	private StudentBean stu;
	private StudentBean1 stu1;
	private StudentBean2 stu2;
	private StudentBean3 stu3;
	static {
		PropertyConfigurator.configure("log4j.properties");
		
	}

	private SqlSession session;

	// 读取配置文件
	private SqlSession all() {
		try {
			Reader reader = Resources.getResourceAsReader("conf/SqlConfig.xml");
			SqlSessionFactory ssf = new SqlSessionFactoryBuilder()
					.build(reader);
			session = ssf.openSession();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return session;
	}

	// 通过映射查询
	public void selectInfo() {
		session = this.all();
		List<StudentBean> list = session.selectList("study.queryinfo", "四川");
		for (StudentBean i : list) {
			System.out.println(i.getS_name() + "--" + i.getS_sex());
		}
		session.close();
	}

	// 通过映射查询(字段名不同)
	public void selectInfo2() {
		all();
		List<StudentBean1> list = session.selectList("gxa.queryinfo", "四川");
		for (StudentBean1 i : list) {
			System.out.println(i.getSname() + "--" + i.getSsex());
		}
		session.close();
	}

	// 通过映射删除
	public void delete() {
		session = this.all();
		int list = session.delete("study.deletes", 1);
		if (list > 0) {
			session.commit();
			System.out.println("删除成功!");
		} else {
			session.rollback();
		}
		session.close();
	}

	// 通过映射添加
	public void Add() {
		session = this.all();
		StudentBean s = new StudentBean();
		s.setS_name("lyx");
		s.setS_age(18);
		s.setS_province("四川");
		s.setS_sex("男");
		int list = session.insert("study.insert", s);
		if (list > 0) {
			session.commit();
			System.out.println("添加成功!");
		} else {
			session.rollback();
		}
		session.close();
	}

	// 通过映射修改
	public void up() {
		session = this.all();
		StudentBean s = new StudentBean();
		s.setS_age(24);
		s.setS_id(3);
		int list = session.update("study.up", s);
		if (list > 0) {
			session.commit();
			System.out.println("添加成功!");
		} else {
			session.rollback();
		}
		session.close();
	}

	public void queryInfo1(String add) {
		session = this.all();
		IMapper map = session.getMapper(IMapper.class);// 实例化接口
		List<StudentBean> list = map.queryinfo(add);
		for (StudentBean s : list) {
			System.out.println(s.getS_name() + "--" + s.getS_id());
		}
		session.close();
	}

	// 通过接口查询
	public void delete1(int id) {
		session = this.all();
		IMapper map = session.getMapper(IMapper.class);// 实例化接口
		int list = map.deletes(id);
		if (list > 0) {
			session.commit();
			System.out.println("删除成功!");
		} else {
			session.rollback();
		}
		session.close();
	}

	// 通过接口添加
	public void Add1(StudentBean s) {
		session = this.all();
		IMapper map = session.getMapper(IMapper.class);
		int list = map.insert(s);
		if (list > 0) {
			session.commit();
			System.out.println("添加成功!");
		} else {
			session.rollback();
		}
		session.close();
	}

	// 通过接口修改
	public void up1(StudentBean s) {
		session = this.all();
		IMapper map = session.getMapper(IMapper.class);
		int list = map.up(s);
		if (list > 0) {
			session.commit();
			System.out.println("修改成功!");
		} else {
			session.rollback();
		}
		session.close();
	}

	// 通过映射1对多
	public void selectOnetomore() {
		session = this.all();
		List<StudentBean2> list = session.selectList("score.queryinfo");
		for (int i = 0; i < list.size(); i++) {
			List<ScoreBean> score = list.get(i).getScore();
			for (int j = 0; j < score.size(); j++) {
				System.out.println(list.get(i).getS_name() + "--"
						+ score.get(j).getSubject() + "--"
						+ score.get(j).getScore());
			}
		}
		session.close();
	}

	// 通过映射一对一
	public void selectmoretomore() {
		session = this.all();
		List<StudentBean3> list = session.selectList("card.queryinfo");
		for (StudentBean3 s : list) {
			System.out.println(s.getS_name() + "--" + s.getCard().getS_card()
					+ "--" + s.getCard().getContent());
		}
		session.close();
	}

	// 动态修改
	public void up1() {
		session = this.all();
		StudentBean sb = new StudentBean();
		sb.setS_province("四川");
		sb.setS_name("lyx1");
		sb.setS_age(18);
		sb.setS_sex("男");
		sb.setS_id(3);
		int flag = session.delete("score.updatesql", sb);
		if (flag > 0) {
			System.out.println("修改成功");
			session.commit();
		} else {
			session.rollback();
		}
		session.close();
	}

	public void se1() {
		session = this.all();
		StudentBean sb = new StudentBean();
		sb.setS_province("四川");
		sb.setS_name("lyx1");
		sb.setS_age(18);
		sb.setS_sex("男");
		sb.setS_id(3);
		List<StudentBean> list = session.selectList("score.selectSql1", sb);
		for (StudentBean s : list) {
			System.out.println(s.getS_name() + "--" + s.getS_province());
		}
	}

	public void se3() {
		try {
			Reader reader = Resources.getResourceAsReader("conf/SqlConfig.xml");
			SqlSessionFactory ssf = new SqlSessionFactoryBuilder()
					.build(reader);
			session = ssf.openSession();
			ssf.getConfiguration().addMapper(IannottationMapper.class);
			IannottationMapper im = session.getMapper(IannottationMapper.class);
			List<StudentBean> list = im.findStudent("四川");
			for (StudentBean s : list) {
				System.out.println(s.getS_name() + "--" + s.getS_age() + "--"
						+ s.getS_province());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			session.close();
		}

	}

	public void se4() {
		Reader reader;
		try {
			reader = Resources.getResourceAsReader("conf/SqlConfig.xml");
			SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
			session = ssf.openSession();
			ssf.getConfiguration().addMapper(IannottationMapper.class);
			IannottationMapper im = session.getMapper(IannottationMapper.class);
			List<StudentBean> list = im.getStudent("四川", "男");
			for (StudentBean s : list) {
				System.out.println(s.getS_name() + "--" + s.getS_sex() + "--"
						+ s.getS_province());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	public CardBean getCard() {
		return card;
	}

	public void setCard(CardBean card) {
		this.card = card;
	}

	public LazystudentBean getLazy() {
		return lazy;
	}

	public void setLazy(LazystudentBean lazy) {
		this.lazy = lazy;
	}

	public ScoreBean getScore() {
		return score;
	}

	public void setScore(ScoreBean score) {
		this.score = score;
	}

	public StudentBean getStu() {
		return stu;
	}

	public void setStu(StudentBean stu) {
		this.stu = stu;
	}

	public StudentBean1 getStu1() {
		return stu1;
	}

	public void setStu1(StudentBean1 stu1) {
		this.stu1 = stu1;
	}

	public StudentBean2 getStu2() {
		return stu2;
	}

	public void setStu2(StudentBean2 stu2) {
		this.stu2 = stu2;
	}

	public StudentBean3 getStu3() {
		return stu3;
	}

	public void setStu3(StudentBean3 stu3) {
		this.stu3 = stu3;
	}

	public SqlSession getSession() {
		return session;
	}

	public void setSession(SqlSession session) {
		this.session = session;
	}
}

 

mybatis基础学习,通过main函数测试数据库读写mybatis基础学习,通过main函数测试数据库读写


打赏

文件名:mybatis.zip,文件大小:6505.316K 下载
最代码最近下载分享源代码列表最近下载
ly3812  LV17 2020年6月5日
咖啡里的糖  LV6 2020年5月19日
yb888888  LV4 2020年4月18日
1358849392  LV21 2019年11月13日
657588854  LV8 2019年7月25日
YoungSpring  LV7 2019年4月19日
wanjiale  LV3 2019年3月18日
caozhou  LV14 2019年3月12日
zhangshao  LV1 2019年3月7日
czp1068894  LV8 2018年11月20日
最代码最近浏览分享源代码列表最近浏览
穿山甲1001  LV4 2023年6月25日
李亮  LV19 2023年3月6日
是你爸爸啊100  LV5 2022年7月30日
ommmly  LV5 2021年6月9日
圣楠123  LV5 2021年3月24日
阿拉斯加  LV5 2021年1月18日
blackcat123  LV7 2020年12月8日
周爱彤  LV2 2020年11月9日
zmy001  LV11 2020年11月2日
anymii  LV1 2020年10月8日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友