MyBatis笔记(二) 最简单的insert命令
接上一篇随笔。这里没有用到MyBatis最关键的映射器接口,因此只做个简单的insert操作,update和delete同理,就不再赘述了。
直接上代码:
首先是dao包下的UserDAO.java文件:
package com.alleymeowy.dao; import com.alleymeowy.bean.User;
import com.alleymeowy.util.DBUtil;
import org.apache.ibatis.session.SqlSession; import java.io.IOException;
import java.util.List; public class UserDAO { /**
* 根据id查询一条记录
* @param id
* @throws IOException
*/
public static User selectOne(int id) throws IOException { SqlSession sqlSession = null;
User user;
try {
sqlSession = DBUtil.getSqlSession();
user = sqlSession.selectOne("com.alleymeowy.config.sql.Users.selectOne", id);
} finally {
sqlSession.close();
}
return user; } /**
* 查询多条记录
* @throws IOException
*/
public static List<User> selectAll() throws IOException { SqlSession sqlSession = null;
List<User> users;
try {
sqlSession = DBUtil.getSqlSession();
users = sqlSession.selectList("com.alleymeowy.config.sql.Users.selectAll");
} finally {
sqlSession.close();
}
return users; } public static void insert(User user) throws IOException { SqlSession sqlSession = null;
try { sqlSession = DBUtil.getSqlSession();
int affectrows = sqlSession.insert("com.alleymeowy.config.sql.Users.insertUser", user);
System.out.println("affectrows : " + affectrows);
sqlSession.commit(); } finally {
if (sqlSession != null) {
sqlSession.close();
}
}
} public static void main(String[] args) throws IOException { // System.out.println(selectOne(1));
// System.out.println("************");
// System.out.println(selectAll()); insert(new User(6, "insertPerson", 100)); }
}
这里添加了一个insert方法。
然后是Users.xml文件,这里存放sql语句。
<?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="com.alleymeowy.config.sql.Users"> <select id="selectOne" parameterType="int" resultType="com.alleymeowy.bean.User">
select id, name, age from user where id = #{id}
</select> <select id="selectAll" resultType="com.alleymeowy.bean.User">
select id, name, age from user
</select> <insert id="insertUser" parameterType="com.alleymeowy.bean.User">
insert into user(id, name, age) values(#{id}, #{name}, #{age})
</insert> </mapper>
同样也是增加了一条insert语句【很简单吧】。
其他代码如总配置文件,DBUtil工具类等都不需要改动了,这里贴出最后的输出结果:

affectrows为1,改变了数据库中的1行。
完毕。
MyBatis笔记(二) 最简单的insert命令的更多相关文章
- MyBatis笔记二:配置
MyBatis笔记二:配置 1.全局配置 1.properites 这个配置主要是引入我们的 properites 配置文件的: <properties resource="db.pr ...
- Mybatis笔记二:接口式编程
目录 旧方法的弊端 接口式编程 接口式编程的好处 接口式编程的增删改查 旧方法的弊端 在Mybatis笔记一中,我们使用命名空间+id的方式实现了Mybatis的执行,不过这里的命名空间是我们随便写的 ...
- how tomcat works 读书笔记(二)----------一个简单的servlet容器
app1 (建议读者在看本章之前,先看how tomcat works 读书笔记(一)----------一个简单的web服务器 http://blog.csdn.net/dlf123321/arti ...
- MyBatis之二:简单增删改查
这一篇在上一篇的基础上简单讲解如何进行增删改查操作. 一.在mybatis的配置文件conf.xml中注册xml与注解映射 <!-- 注册映射文件 --> <mappers> ...
- Mybatis笔记二
一对一查询 案例:查询所有订单信息,订单信息中显示下单人信息. 注意:因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询.如果从用户信息出发查询用户下的订单信息则 ...
- Mybatis笔记二:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
错误异常:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.test.dao.N ...
- mybatis笔记<二> 整合spring
mybatis与spring整合需要添加几个jar包,mybatis-spring, spring-context, spring-jdbc 1. spring ioc只要一个jar包就ok 2. 我 ...
- 吴裕雄--天生自然HADOOP操作实验学习笔记:hdfs简单的shell命令
实验目的 了解bin/hadoop脚本的原理 学会使用fs shell脚本进行基本操作 学习使用hadoop shell进行简单的统计计算 实验原理 1.hadoop的shell脚本 当hadoop集 ...
- MyBatis 学习二之简单练习巩固
1.新建一个maven项目并在pom.xml中添加依赖 2.项目架构 配置文件:SqlMapConfig.xml <?xml version="1.0" encoding ...
随机推荐
- Android常用的工具类SharedPreferences封装类SPUtils
package com.zhy.utils; import java.lang.reflect.InvocationTargetException; import java.lang.reflect. ...
- CentOS7 安装 jexus-5.8.2-x64
前要提示: 如果你要安装asp.net 请参考: http://www.cnblogs.com/xiaoruilin/p/7867823.html 背景: CentOS7 Mono (Mono JIT ...
- dhtmlx Gantt知识点1
鼠标放在任务上显示信息框: <script src="../../codebase/ext/dhtmlxgantt_tooltip.js?v=5.2.0"></s ...
- logback使用注意点1
logback中配置了springProfile(策略),因此在properties中只需要配置如下即可logging.config=./config/logback.xml //logback配置文 ...
- 好大一个坑: EF Core 异步读取大字符串字段比同步慢100多倍
这两天遇到一个奇怪的问题,通过 EF/EF Core 查询数据库速度奇慢,先是在传统的 ASP.NET 项目中遇到(用的是EF6.0),后来将该项目迁移至 ASP.NET Core 也是同样的问题(用 ...
- js自定义格式化时间戳的格式
题目为 : 写一个模块,外部调用这个模块,请求参数是时间戳,模块要求 今天的时间,统一用24小时写作 03:00.15:04 昨天的时间,统一写昨天 昨天之前的时间,但在本周之内的时间,统一用周一.周 ...
- [vue开发记录]全局loading组件
上图不上种,菊花万人捅: loading.js: import './loading.css' let Loading = {} // 避免重复install,设立flag Loading.insta ...
- thinkphp ckeditor与ckfinder
thinkphp ckeditor与ckfinder 下载 ckeditor下载地址 ckfinder下载地址 整合 将ckeditor与findeditor下载完成后,放到public目录下,配置c ...
- VisualStudioCode创建的asp.net core项目部署到IIS,以及遇到的问题
一.发布项目 在visual studio code中通过命令“dotnet publish”,如下图: 这里我把发布位置设置到了D:\WebSite\netcoredemo下. 二.设置IIS 0. ...
- LAYUI select 下拉框得高度
页面下得select 框 在css页面加样式 .layui-form-select dl { max-height: 152px;}