mybatis初级增删改查-最全

mybatis.xml的代码:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入约束(引入这个东西后,这个配置文件则有了一些提示标签,并且有一些语法校验) -->
<!-- 提示alt+/ -->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<!-- configuration中的字标签是有顺序的,不能颠倒 -->
	<!-- 设置配置文件位置 -->
	<properties resource="com/zx/mybatis/properties/jdbc.properties"></properties>
	<!-- 用来配置mybatis别名 -->
	<typeAliases>
		<!-- package指定的包下所有的类都可以直接用类名表示,而不需要再写全路径 -->
		<package name="com.zx.mybatis.entity"/>
		<!-- 二选一,不能同时存在 -->
		<!-- <typeAlias type="com.zx.mybatis.entity.Student" alias="stu" /> -->
	</typeAliases>
	<!-- 用来配置环境的(连接参数) -->
	<environments default="zy">
		<environment id="zy">
			<!-- 将事务交由jdbc管理 -->
			<transactionManager type="JDBC"></transactionManager>
			<dataSource type="POOLED">
				<!-- ${}语法是从.properties配置文件中获取对应值 -->
				<property name="driver" value="${jdbc.driver}"/>
				<property name="url" value="${jdbc.url}"/>
				<property name="username" value="${jdbc.username}"/>
				<property name="password" value="${jdbc.password}"/>
			</dataSource>
		</environment>
	</environments>
	<!-- 配置映射xml配置文件路径 -->
	<mappers>
		<!-- 
			resource:用来配置相对路径xml文件
			url:用来配置绝对路径xml文件
			class:用来配置类路径
		 -->
		 <mapper resource="com/zx/mybatis/mapper/StudentMapper.xml"/>
		 <!-- package配置多个(类路径) -->
		 <!-- <package name=""/> -->
	</mappers>
</configuration>

jdbc.properties的代码:

jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=duay
jdbc.password=123456

java实体类代码:

package com.zx.mybatis.entity;
/**
 * 学生类
 * @author zhangyi
 *
 */
public class Student {
	private int id;
	private String name;
	private int age;
	private double score;
	
	public Student() {
		super();
	}

	public Student(int id, String name, int age, double score) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.score = score;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public double getScore() {
		return score;
	}

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

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", score=" + score + "]";
	};
}

mapper.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.xml的父标签 namespace+id==类名+方法名 -->
<mapper namespace="a.b.c">
	<insert id="xzStudent" parameterType="Student">
		<!-- 注意:sql结尾不要加分号 -->
		insert into STUDENT (ID,NAME,AGE,SCORE) values (#{id}, #{name}, #{age}, #{score})
	</insert>
	<!-- 
		parameterType:参数类型
		resultType:返回值类型
	 -->
	<select id="cxStudent" parameterType="_int" resultType="Student">
		<!-- 注意:sql结尾不要加分号 -->
		<!-- 当只有一个参数时,参数名可以任意写 -->
		select id, name, age, score from student where id = #{iidd}
	</select>
	<update id="gxStudent" parameterType="Student">
		update student set name=#{name}, age=#{age}, score=#{score} where id=#{id}
	</update>
	<delete id="scStudent" parameterType="_int">
		delete from student where id=#{id}
	</delete>
	<!-- 查所有List,resultType中的返回值类型写List中的泛型 -->
	<select id="selectListStudent" resultType="Student">
		select id, name, age, score from student
	</select>
	<!-- 查所有Map,resultType中的返回值类型写Map中value的泛型 -->
	<select id="selectMapStudent" resultType="Student">
		select id, name, age, score from student
	</select>
</mapper>

java测试代码:

package com.zx.mybatis.test;

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

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.junit.Test;

import com.zx.mybatis.entity.Student;
import com.zx.mybatis.util.MybatisUtil;

public class Test1 {
	@Test
	public void Test01(){
		//主配置文件的位置
		String resource = "mybatis.xml";
		//加载对应路径下的配置文件到流中
		InputStream inputStream = null;
		try {
			inputStream = Resources.getResourceAsStream(resource);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//sqlSession工厂:用来创建sqlSession
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		//创建sqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		/*
		 * 调用sqlSession的新增方法
		 * statement:mapper.xml的位置(namespace+id)
		 * parameter:参数
		 * result:这条sql影响的条目数
		 */
		int result = sqlSession.insert("a.b.c.xzStudent", new Student(11, "李四", 23, 90.01));
		//提交
		sqlSession.commit();
		System.out.println(result);
		//释放资源
		sqlSession.close();
	}
	@Test
	public void Test02(){
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		Student stu = sqlSession.selectOne("a.b.c.cxStudent", 1);
		System.out.println(stu);
		sqlSession.close();
	}
	@Test
	public void Test03() {
		//update
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		int result = sqlSession.update("a.b.c.gxStudent", new Student(11, "李四123", 23, 90.01));
		System.out.println(result);
		sqlSession.commit();
		sqlSession.close();
	}
	@Test
	public void Test04() {
		//delete
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		int result = sqlSession.delete("a.b.c.scStudent", 11);
		System.out.println(result);
		sqlSession.commit();
		sqlSession.close();
	}
	@Test
	public void Test05() {
		//selectList
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		List<Student> students = sqlSession.selectList("a.b.c.selectListStudent");
		for(Student s : students) {
			System.out.println(s);
		}
		sqlSession.close();
	}
	@Test
	public void Test06() {
		//selectMap
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		Map<Integer, Student> map = sqlSession.selectMap("a.b.c.selectMapStudent", "age");
		//遍历map
		Set<Map.Entry<Integer, Student>> entrySet = map.entrySet();
		for(Map.Entry<Integer, Student> entry : entrySet) {
			System.out.println("key:"+entry.getKey()+",value:"+entry.getValue().toString());
		}
		sqlSession.close();
	}
}

java工具类:

package com.zx.mybatis.util;

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 MybatisUtil {
	
	private static String resource = "mybatis.xml";
	private static SqlSessionFactory sqlSessionFactory = null;
	/**
	 * 获取sqlSession
	 * @return
	 */
	public static SqlSession getSqlSession() {
		if(sqlSessionFactory==null) {
			try {
				InputStream inputStream = Resources.getResourceAsStream(resource);
				sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		/*
		 * 调用sqlSession工厂创建一个SqlSession
		 * 它有重载的方法,用来控制事务是否自动提交
		 * 默认是不自动进行事务提交的
		 */
		return sqlSessionFactory.openSession();
	}
}

共同探讨学习技术创建技术氛围Day9884125

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值