mybatis高级(1)(入门回顾)
首先入门案例(并且拿到新增记录当前id)
1.创建mybatis-config.xml文件
<?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>
<!-- 使用别名 -->
<typeAliases>
<!-- 为包下所有类使用别名,默认是简单类名 -->
<package name="cn.cnsdhzzl.entity" />
<!-- 对单个类,按类型名使用别名 -->
<!-- <typeAlias type="cn.cnsdhzzl.entity.Student" alias="student" /> -->
</typeAliases> <!-- 开发模式 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc的事务 -->
<transactionManager type="JDBC" />
<!-- 使用自带的连接池 -->
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="**" />
<property name="password" value="**" />
</dataSource>
</environment>
</environments> <!-- 映射小配置 -->
<mappers>
<mapper resource="cn/cnsdhzzl/dao/StudentDAO.xml" />
</mappers>
</configuration>
2.创建分层搭建架构

3.创建StudentDAO.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.cnsdhzzl.dao">
<!-- 添加操作 -->
<insert id="insertStudent">
insert into student(id,name,address,sex)
values(seq_ssm.nextval,#{name},#{address},#{sex})
<selectKey keyProperty="id" resultType="int">
select
seq_ssm.currval from dual
</selectKey>
</insert> <!-- 删除的id名称随便写 -->
<delete id="deleteStudentById">
delete student where id=#{id}
</delete> <!-- 修改操作 -->
<update id="updateStudent">
update student set
name=#{name},address=#{address},sex=#{sex} where id=#{id}
</update> <!-- 返回集合要加resultType -->
<select id="findAll" resultType="student">
select * from student
</select> <!-- 方法一 -->
<select id="likeSelectByStr" resultType="student">
select * from student
where name like '%${value}%'
</select>
<!-- 方法二 -->
<!-- <select id="likeSelectByStr" resultType="student"> select * from student
where name like concat('%',#{stuname},'%') </select> --> <!-- 方法一 -->
<select id="likeSelectByEntity" resultType="student">
select * from student
where name like '%${name}%'
</select>
<!-- 方法二 -->
<!-- <select id="likeSelectByEntity" resultType="student"> select * from
student where name like concat('%',#{stuname},'%') </select> --> </mapper>
4.实现接口方法
@Override
public Integer addStudent(Student stu) {
SqlSession sqlSession = SessionUtil.getSqlSession();
int result = sqlSession.insert("insertStudent", stu); sqlSession.commit();
sqlSession.close(); System.out.println("保存结果" + result);
return result;
}
5.测试
@Test
public void frist() {
Student stu = new Student("BBB", "北京", "男");
StudentDaoImpl dao = new StudentDaoImpl();
dao.addStudent(stu);
System.out.println("成功");
}
结果图

注意事项:
语句后面不能加';'(分号),会报sql异常
mybatis高级(1)(入门回顾)的更多相关文章
- MyBatis高级篇之整合ehcache缓存框架
MyBatis高级篇之整合ehcache缓存框架 2017-09-01 0 Comments 1,671 Views 0 Times 一.前言 MyBatis为我们提供了Cache接口,也提供 ...
- 【Mybatis高级映射】一对一映射、一对多映射、多对多映射
前言 当我们学习heribnate的时候,也就是SSH框架的网上商城的时候,我们就学习过它对应的高级映射,一对一映射,一对多映射,多对多映射.对于SSM的Mybatis来说,肯定也是差不多的.既然开了 ...
- 8、web入门回顾/ Http
1 web入门回顾 web入门 1)web服务软件作用: 把本地资源共享给外部访问 2)tomcat服务器基本操作 : 启动: %tomcat%/bin/startup.bat 关闭: % ...
- mybatis高级映射(一对一,一对多)
mybatis高级映射 一对一关联映射 需求:查询订单信息,关联查询用户信息(一个订单对应一个用户) (1)通过resultType实现 sql语句: select orders.* , USER.u ...
- MyBatis(1)——快速入门
MyBatis 简介 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为 ...
- (转) MyBatis(1)——快速入门
MyBatis 简介 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为 ...
- MyBatis高级查询
-------------------------siwuxie095 MyBatis 高级查询 1.MyBatis 作为一个 ORM 框架,也对 SQL 的高级查询做了支持, MyBatis 高级查 ...
- Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建
目录 Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建 0.项目准备 1.数据持久层Mybatis+MySQL 1.1 MySQL数据准备 1.2 Mybatis ...
- (转)MyBatis框架的学习(二)——MyBatis架构与入门
http://blog.csdn.net/yerenyuan_pku/article/details/71699515 MyBatis框架的架构 MyBatis框架的架构如下图: 下面作简要概述: S ...
随机推荐
- SQL Server 在task manager里面显示CPU 使用率过高
发现一篇好文章 https://mssqlwiki.com/2012/10/04/troubleshooting-sql-server-high-cpu-usage/
- C# 集合已修改;可能无法执行枚举操作
在winform 项目时遇到: 集合已修改;可能无法执行枚举操作的问题 错误原因:当用foreach遍历Collection时,如果对Collection有Add或者Remove或其他类似操作都会有这 ...
- angularjs的三目运算
前言:前几天写代码的时候遇到一个问题,有一个按钮,有"已关注"和"+关注"两种状态,需要对这两种状态的按钮的背景颜色进行区分,单后点击"已关注&quo ...
- centos 更换 yum源
阿里云Linux安装镜像源地址:http://mirrors.aliyun.com/ CentOS系统更换软件安装源第一步:备份你的原镜像文件,以免出错后可以恢复. mv /etc/yum.repos ...
- asp.net调用客户端WebBrowser 进行网站地址截屏
在asp.net网站中,如果要实现,在文本框中输入一个URL地址,就把该网页的页面整屏截下来,这段时间一直在研究这一块,在网上查了好多资料.自己又整合了一下. 其实也不是想象中的那么难.主要是通过调用 ...
- iOS中如何隐藏启动图片的状态栏
只隐藏启动图片顶部的状态栏,而不影响程序运行起来控制器的状态栏显示?在info.plist文件中添加一个Statis bar is...... 默认是NO改为YES即可
- 20160626001 O2O Website
1. http://sidapai.baijia.baidu.com/ 百度百家 2. http://www.huxiu.com/ 虎嗅网 3. http://i.wshang.com ...
- 课堂 表单验证 JavaScript js
页面加载完才开始执行代码要加上 window.onload = function{ } 获取元素节点 var uesrname = document.getElementById("user ...
- jQuery validation学习(1)验证只输入空格通过验证
当input输入了空格是不会提示信息的 一般会去除空格然后进行验证 这个时候就要添加onkeyup事件去除左侧的空格 验证只输入空格通过验证 //添加验证手机方法 jQuery.validator.a ...
- 【前端】原生event对象和jquery event对象的区别
标准DOM event对象转换成 jQuery event对象 $(event) jQuery event对象转换成 标准DOM event对象 event.originalEvent