MyBatis的CRUD操作
MyBatis的两个主要配置文件
mytatis.xml:放在src目录下,常见的配置如下
<?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>
<!--1-->
<properties resource="db.properties"/>
<!--2-->
<typeAliases>
<typeAlias type="com.winner.entity.Student" alias="Student"/>
</typeAliases>
<!--3-->
<environments default="mysql_developer">
<environment id="mysql_developer">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
<environment id="oracle_developer">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</dataSource>
</environment>
</environments>
<!--4-->
<mappers>
<mapper resource="com/winner/entity/StudentMapper.xml"/>
</mappers>
</configuration>
SQL语句后面不要有;
StudentMapper.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"> <!--namespace写成类的全限定名有好处,在Dao中方便-->
<mapper namespace="com.winner.entity.Student"> <!--type是类的全限定名,因为mybatis.xml中有别名的设置,所以用别名,短,方便-->
<resultMap id="studentMap" type="Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sal" column="sal"/>
</resultMap> <!--方法无参数-->
<insert id="add1">
<![CDATA[
INSERT INTO student(id,name,sal) VALUES (1,"zhangsan",2000)
]]>
</insert> <!--type是类的全限定名,因为mybatis.xml中有别名的设置,所以用别名,短,方便-->
<insert id="add2" parameterType="Student">
<![CDATA[
INSERT INTO student(id,name,sal) VALUES (#{id},#{name},#{sal})
]]>
</insert>
</mapper>
1.增加
StudentMapper.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"> <!--namespace写成类的全限定名有好处,在Dao中方便-->
<mapper namespace="com.winner.entity.Student"> <!--type是类的全限定名,因为mybatis.xml中有别名的设置,所以用别名,短,方便-->
<resultMap id="studentMap" type="Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sal" column="sal"/>
</resultMap> <!--type是类的全限定名,因为mybatis.xml中有别名的设置,所以用别名,短,方便-->
<insert id="add" parameterType="Student">
<![CDATA[
INSERT INTO student(id,name,sal) VALUES (#{id},#{name},#{sal})
]]>
</insert>
</mapper>
public class StudentDao {
/**
* 添加学生
*/
public void add(Student student) throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
//事务开始(默认)
//读取StudentMapper.xml映射文件中的SQL语句
//insert的第一个参数:
// namespace写实体类的全限定名就是这个好处
sqlSession.insert(Student.class.getName() + ".add", student);
//事务提交
sqlSession.commit();
}catch (Exception e){
e.printStackTrace();
//事务回滚
sqlSession.rollback();
throw e;
}finally {
MybatisUtil.closeSqlSession();
}
}
public static void main(String[] args) throws Exception{
StudentDao dao = new StudentDao();
dao.add(new Student(1,"zhansan",1000d));
}
}
2.根据ID查询学生
<!--
根据ID查询学生
如果参数不是一个实体的话,只是一个普通变量,例如:int,double,String
这里的#{中间的变量名可以随便写},不过提倡就用方法的形参.
resultType是方法返回值类型
-->
<select id="findById" parameterType="int" resultType="Student">
SELECT id,name,sal FROM student WHERE id=#{id}
</select>
/**
* 根据ID查询学生
*/
public Student findById(int id) throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
Student student = sqlSession.selectOne(Student.class.getName() + ".findById", id);
sqlSession.commit();
return student;
}catch (Exception e){
e.printStackTrace();
//事务回滚
sqlSession.rollback();
throw e;
}finally {
MybatisUtil.closeSqlSession();
}
}
3.查询所有
<!-- 查询所有学生
理论上resultType要写List<Student>
但这里只需书写List中的类型即可,即只需书写Student的全路径名
-->
<select id="findAll" resultType="student">
SELECT id,name,sal FROM student
</select>
/**
* 查询所有学生
*/
public List<Student> findAll() throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
return sqlSession.selectList(Student.class.getName()+".findAll");
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
MybatisUtil.closeSqlSession();
}
}
4.更新学生
<!-- 更新学生 -->
<update id="update" parameterType="Student">
UPDATE student SET name=#{name},sal=#{sal} WHERE id=#{id}
</update>
/**
* 更新学生
*/
public void update(Student student) throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
sqlSession.update(Student.class.getName()+".update",student);
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e;
}finally{
MybatisUtil.closeSqlSession();
}
}
Student student = studentDao.findById(3);
student.setName("wangwuwu");
studentDao.update(student);
5.删除
<!-- 删除学生 -->
<delete id="delete" parameterType="student">
DELETE FROM student WHERE id = #{id}
</delete>
/**
* 删除学生
*/
public void delete(Student student) throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
sqlSession.delete(Student.class.getName()+".delete",student);
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e;
}finally{
MybatisUtil.closeSqlSession();
}
}
Student student = dao.findById(3);
dao.delete(student);
MyBatis的CRUD操作的更多相关文章
- 【MyBatis】MyBatis实现CRUD操作
1.实现基本CRUD功能 使用MyBatis对数据完整的操作,也就是CRUD功能的实现.根据之前的内容,要想实现CRUD,只需要进行映射文件的配置. 范例:修改EmpMapper.xml文件,实现CR ...
- 05 Mybatis的CRUD操作和Mybatis连接池
1.CRUD的含义 CRUD是指在做计算处理时的增加(Create).读取(Retrieve)(重新得到数据).更新(Update)和删除(Delete)几个单词的首字母简写.主要被用在描述软件系统中 ...
- Spring boot 入门四:spring boot 整合mybatis 实现CRUD操作
开发环境延续上一节的开发环境这里不再做介绍 添加mybatis依赖 <dependency> <groupId>org.mybatis.spring.boot</grou ...
- Mybatis:CRUD操作
提示: Mapper配置文件的命名空间为对应接口包名+接口名字,这个经常会忘记和搞错的!! select标签 在接口中编写三个查询方法 //获取全部用户List<User> selectU ...
- MyBatis学习01(初识MyBatis和CRUD操作实现)
1.初识MyBatis 环境说明: jdk 8 + MySQL 5.7.19 maven-3.6.1 IDEA 学习前需要掌握: JDBC MySQL Java 基础 Maven Junit 什么是M ...
- mybatis中crud操作范例
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- java之mybatis之使用mybatis实现crud操作
目录结构: 1.封装 mybatis 的工具类: MybatisUtil.java public class MybatisUtil { private static SqlSessionFactor ...
- SSM框架之Mybatis(2)CRUD操作
Mybatis(2)CRUD 1.基于代理Dao实现CRUD操作 使用要求: 1.持久层接口(src\main\java\dao\IUserDao.java)和持久层接口的映射配置(src\main\ ...
- Mybatis的CRUD案例
一.Mybatis增删改查案例 上一节<Mybatis入门和简单Demo>讲了如何Mybatis的由来,工作流程和一个简单的插入案例,本节主要继上一讲完整的展示Mybatis的CRUD操作 ...
随机推荐
- 查看cpu、内存和硬盘
查看cpu cat /proc/cpuinfo 查看内存 top free -m 按兆为单位输出内存的已用,未用,总共等结果 cat /proc/meminfo |grep MemTotal 查看硬盘 ...
- 暑假集训(5)第二弹———湫湫系列故事——减肥记I(hdu4508)
问题描述:舔了舔嘴上的油渍,你陷在身后柔软的靠椅上.在德源大赛中获得优胜的你,迫不及待地赶到“吃到饱”饭店吃到饱.当你 正准备离开时,服务员叫住了你,“先生,您还没有吃完你所点的酒菜.”指着你桌上的一 ...
- linux 运维知识体系
这里将会介绍一下,LINUX运维工程师的知识体系. 只能说是个人理解吧.并不是必要或者充分的,仅供网友参考. 大部分本博客都有涉及,并不完整. 1.LINUX运维基础 1.1.LINUX系统的简介,分 ...
- php学习日志(2)-php变量
变量是用于存储数据的容器,与代数相似,可以给变量赋予某个确定的值(例如:$x=3)或者是赋予其它的变量(例如:$x=$y+$z).变量的定义主要有以下规则: 变量以$开始,后面跟着变量的名称: 变量名 ...
- php字符串首字母转换大小写的实例分享
php中对字符串首字母进行大小写转换的例子. in: 后端程序首字母变大写:ucwords() <?php $foo = 'hello world!'; $foo = ucwords($foo) ...
- ARM-Linux S5PV210 UART驱动(4)----串口驱动初始化过程
对于S5PV210 UART驱动来说,主要关心的就是drivers/serial下的samsung.c和s5pv210.c连个文件. 由drivers/serial/Kconfig: config S ...
- Java 类和对象
主要参考文献:王映龙<Java程序设计> 一:类的语法 [修饰符]class<类名>[extends父类名][implements接口列表]{ //类体} 1:修饰符 可选值为 ...
- Oracle的AUTOTRACE功能
ORACLE9i在使用autotrace之前,需要作一些初始设置: 1.用sys用户运行脚本utlxplan.sql创建PLAN_TABLE表 脚本目录:(UNIX:$ORACLE_HOME/rdbm ...
- iOS:横向使用iPhone默认的翻页效果
大致思路使用两层辅助UIView的旋转来实现添加后的View的横向翻页效果 CATransform3D transformA = CATransform3DRotate(CATransform3DId ...
- [译] ASP.NET 生命周期 – ASP.NET 上下文对象(八)
使用 HttpResponse 对象 HttpResponse 对象是与 HttpRequest 对象相对应的,用来表示构建中的响应.它当中提供了方法和属性可供我们自定义响应,有一些在使用 MVC 视 ...