0 -MyBaties基础实现(xml文件)

1 - jar包

mybaties官网下载

2 - 配置

在javaBean文件包下

<?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.xml映射文件的唯一标识 -->
<mapper namespace="com.qsd.entity.studentMapper">
	<!-- 
		paramterType:输入参数的类型
		resultType:返回值类型
	 -->
	 <!-- 查询根据学号 -->
	<select id="selectStudentBySno" resultType="com.qsd.entity.Student" parameterType="int" >
		select * from student where sno = #{sno}
	</select>
	<!-- 增加 -->
	<insert id="insertStudent" parameterType="com.qsd.entity.Student">
		insert into student values(#{sno},#{name},#{age},#{hobby})
	</insert>
	<!-- 修改 -->
	<update id="updataStudentBySno" parameterType="com.qsd.entity.Student" >
		update student set name = #{name} ,age = #{age} ,hobby = #{hobby} where sno = #{sno}
	</update>
	<!-- 删除 -->
	<delete id="deleteStudentBySno" parameterType="int">
		delete from student where sno = #{sno}
	</delete>
	<!-- 查询全部学生 -->
	<select id="selectAllStudent" resultType="com.qsd.entity.Student">
		select * from student
	</select>
</mapper>

MyBaties约定

输入参数paramentType和输出参数resultType,在形式上只能有一个
如果输入输出参数是简单类型,是可以使用任意占位符#{xxx}
如果是对象类型,则只能是对象的属性#{属性名}

在根目录下

<?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>
	<environments default="development">
		<!-- 通过default指定id的配置信息 -->
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<!-- 配置数据库信息 -->
				<property name="driver" value="com.mysql.cj.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost/dbconn?serverTimezone=UTC" />
				<property name="username" value="root" />
				<property name="password" value="rsrzcxj521" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
	<!-- 加载映射文件 -->
		<mapper resource="com/qsd/entity/studentMapper.xml" />
	</mappers>
</configuration>

使用

package com.qsd.main;

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 com.qsd.entity.Student;

public class test {
	private static final String namespcae = "com.qsd.entity.studentMapper.";
	private static final String id1 = "selectStudentBySno";
	private static final String id2 = "selectAllStudent";

	public static void main(String[] args) throws Exception {
		// TODO 自动生成的方法存根
		Student student = new Student(3,"Pater",22,"swimming");
		int count = insert("insertStudent", student);
		System.out.println("增加了"+count+"名学生");
		selectAll("selectAllStudent");
	}
	public static void selectOne(String id,int sno) throws Exception {
		SqlSession openSession = getSqlSession();
		//查询
		Student student = openSession.selectOne(namespcae+id,sno);
		System.out.println(student);
		openSession.close();
	}
	public static void selectAll(String id) {
		try {
			//加载MyBaties配置文件(为了访问数据库)
			Reader resourceAsReader = Resources.getResourceAsReader("config.xml");
			// Building SqlSessionFactory from XML
			SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsReader);
			//session -> connection
			SqlSession openSession = sessionFactory.openSession();
			//查询
			List<Student> students = openSession.selectList(namespcae+id);
			for(Student student : students) {
				System.out.println(student);
			}
			openSession.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	public static SqlSession getSqlSession() throws Exception {
		Reader resourceAsReader = Resources.getResourceAsReader("config.xml");
		// Building SqlSessionFactory from XML
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsReader);
		//session -> connection
		SqlSession openSession = sessionFactory.openSession();
		return openSession;
	}
	public static int insert(String id,Student student) throws Exception {
		SqlSession session = getSqlSession();
		int count = session.insert(namespcae+id,student);
		session.commit();
		return count>0?count:0;
	}
}

3 - 目录结构

目录结构
注意:com.qsd.converter是转换器,与本文无关

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值