mybatis学习(29):适用于没有自增的数据库
目录结构
com.geyao.mybatis.mapper
BlogMapper类
package com.geyao.mybatis.mapper;import java.util.List;
import java.util.Map;import org.apache.ibatis.annotations.Param;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(Integer id);Blog selectBlog2(Integer id);List<Blog> selectBlogByTitle(String title);List<Blog> selectBlogByTitle2(String title);List<Blog> selectBlogBySort(String column);List<Blog> selectBlogByPage(int offset,int pagesize);List<Blog> selectBlogByPage1(@Param(value="offset")int offset,@Param(value="pagesize")int pagesize);List<Blog> selectBlogByPage2(Map<String, Object>map);int insertBlog(Blog blog);int insertBlogMysql(Blog blog);
}
BlogMapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)-->
<mapper namespace="com.geyao.mybatis.mapper.BlogMapper"><!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回User类就是users表所对应的实体类--><!-- 根据id查询得到一个user对象--><resultMap type="Blog" id="blogResultMap"><id column="id" property="id" jdbcType="INTEGER"></id><result column="authod_id" property="authodId" jdbcType="INTEGER"/></resultMap><select id="selectBlog" parameterType="int" resultType="Blog">select id,title,authod_id as authodId,state,featured,stylefrom Blog where id=#{id}</select> <select id="selectBlog2" parameterType="int" resultMap="blogResultMap">select *from Blog where id=#{id}</select><select id="selectBlogByTitle" parameterType="String" resultMap="blogResultMap">select * from Blog where title like #{title}</select><select id="selectBlogByTitle2" parameterType="String" resultMap="blogResultMap">select * from Blog where title like '${value}'</select><select id="selectBlogBySort" parameterType="String" resultMap="blogResultMap">select * from Blog order by ${value}</select><select id="selectBlogByPage" resultMap="blogResultMap">select * from Blog limit #{0},#{1}</select><select id="selectBlogByPage1" resultMap="blogResultMap">select * from Blog limit #{offset},#{pagesize}</select><select id="selectBlogByPage2" resultMap="blogResultMap">select * from Blog limit #{offset},#{pagesize}</select><insert id="insertBlog" parameterType="Blog" useGeneratedKeys="true" keyProperty="id">INSERT INTO Blog(title,authod_id,state,featured,style)VALUES(#{title},#{authodId},#{state},#{featured},#{style})</insert><insert id="insertBlogOracle" parameterType="Blog" ><selectKey resultType="java.lang.Integer" order="BEFORE" keyProperty="id">select seq.nextval as id from dual</selectKey>INSERT INTO Blog(title,authod_id,state,featured,style)VALUES(#{title},#{authodId},#{state},#{featured},#{style})</insert><insert id="insertBlogMysql" parameterType="Blog" ><selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">SELECT LAST_INSERT_ID()</selectKey>INSERT INTO Blog(title,authod_id,state,featured,style)VALUES(#{title},#{authodId},#{state},#{featured},#{style})</insert>
</mapper>
com.geyao.mybatis.pojo
Blog类
package com.geyao.mybatis.pojo;public class Blog {private Integer id;private String title;private int authodId;private String state;private Boolean featured;private String style;public Blog() {super();this.title="未命名";this.authodId=4;this.state="NOT";this.featured=false;this.style="red"; }public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public int getAuthodId() {return authodId;}public void setAuthodId(int authodId) {this.authodId = authodId;}public String getState() {return state;}public void setState(String state) {this.state = state;}public Boolean getFeatured() {return featured;}public void setFeatured(Boolean featured) {this.featured = featured;}public String getStyle() {return style;}public void setStyle(String style) {this.style = style;}@Overridepublic String toString() {return "Blog [id=" + id + ", title=" + title + ", authodId=" + authodId + ", state=" + state + ", featured="+ featured + ", style=" + style + "]\n";}}
com.geyao.mybatis.util
MybatisUtil类
package com.geyao.mybatis.util;import java.io.InputStream;
import java.io.Reader;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 SqlSessionFactory sqlSessionFactory =null;static {try {InputStream in = Resources.getResourceAsStream("mybatis-config.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}private MyBatisUtil() {}public static SqlSession getSqlSession() {return sqlSessionFactory.openSession();}
}
log4j.properties
### \u914D\u7F6E\u6839 ###
log4j.rootLogger = debug,console ,fileAppender,dailyRollingFile,ROLLING_FILE,MAIL,DATABASE### \u8BBE\u7F6E\u8F93\u51FAsql\u7684\u7EA7\u522B\uFF0C\u5176\u4E2Dlogger\u540E\u9762\u7684\u5185\u5BB9\u5168\u90E8\u4E3Ajar\u5305\u4E2D\u6240\u5305\u542B\u7684\u5305\u540D ###
log4j.logger.org.apache=dubug
log4j.logger.java.sql.Connection=dubug
log4j.logger.java.sql.Statement=dubug
log4j.logger.java.sql.PreparedStatement=dubug
log4j.logger.java.sql.ResultSet=dubug### \u914D\u7F6E\u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n
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><typeAlias type="com.geyao.mybatis.pojo.Blog" alias="Blog"/>
</typeAliases><environments default="development"><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:3306/mybatis?serverTimezone=GMT%2B8" /><property name="username" value="root" /><property name="password" value="123" /></dataSource></environment></environments><mappers><!-- 注册userMapper.xml文件, userMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/userMapper.xml--><mapper resource="com/geyao/mybatis/mapper/BlogMapper.xml"/></mappers>
</configuration>
单元测试
com.geyao.mybatis.util
testSelectBlog类
package com.geyao.mybatis.mapper;import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import com.geyao.mybatis.pojo.Blog;
import com.geyao.mybatis.util.MyBatisUtil;public class testSelectBlog {@Testpublic void testSelectBlogNoInterface() {SqlSession session =MyBatisUtil.getSqlSession();Blog blog =(Blog)session.selectOne("com.geyao.mybatis.mapper.BlogMapper.selectBlog", 101);session.close();System.out.println(blog);}
@Test
public void testSelectBlog() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog = blogMapper.selectBlog(1);System.out.println(blog);
}@Test
public void testSelectBlog2() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog = blogMapper.selectBlog2(1);System.out.println(blog);
}
@Test
public void testselectBlogByTitle() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogByTitle("%g%");System.out.println(blogList);
}@Test
public void testselectBlogByTitle2() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogByTitle2("%g%");System.out.println(blogList);
}
@Test
public void testselectBlogBySort() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogBySort("title");System.out.println(blogList);
}
@Test
public void testselectBlogByPage() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogByPage(2,2);System.out.println(blogList);
}
@Test
public void testselectBlogByPage1() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogByPage1(2,2);System.out.println(blogList);
}
@Test
public void testselectBlogByPage2() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Map<String, Object> map =new HashMap<String, Object>();map.put("offset", 2);map.put("pagesize", 2);List<Blog> blogList = blogMapper.selectBlogByPage2(map);System.out.println(blogList);
}@Test
public void testInsertBlog() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog=new Blog();int count= blogMapper.insertBlog(blog);session.commit();session.close();System.out.println(blog);System.out.println("插入的"+count+"记录");
}@Test
public void testinsertBlogMysql() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog=new Blog();int count= blogMapper.insertBlogMysql(blog);session.commit();session.close();System.out.println(blog);System.out.println("插入的"+count+"记录");
}
}
jar包
链接:https://pan.baidu.com/s/1g6NgzfLc5uK9S4VL-03lHg
提取码:4r2m
运行结果
mybatis学习(29):适用于没有自增的数据库相关推荐
- Mybatis学习笔记(二) 之实现数据库的增删改查
开发环境搭建 mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包.这些软件工具均可以到各自的官方网站上下载 ...
- Mybatis学习笔记【part03】增删改查
Mybatis 学习笔记 Part03 1.传入参数与返回参数设置(查) <!-- 根据 id 查询 --> <select id="findById" resu ...
- MyBatis学习总结(三)
MyBatis学习总结(三) 之前对数据库的增删改查是基于XML实现的,现在要基于注解实现. 1 .定义SQL映射接口 这个是我的项目目录 创建一个接口UserMapperI接口 package co ...
- MyBatis学习笔记(二)根据配置文件优化
上一篇:MyBatis学习笔记(一)完整查询数据库流程+增删改查 mybatis_config.xml文件中可以包含以下部分 本文接下来的优化都是针对于mybatis_config.xml文件,并且每 ...
- MyBatis学习--简单的增删改查
jdbc程序 在学习MyBatis的时候先简单了解下JDBC编程的方式,我们以一个简单的查询为例,使用JDBC编程,如下: 1 Public static void main(String[] arg ...
- Mybatis学习错误之:重复加载mapper.xml
学习mybatis的时候,突然遇到测试出错.测试mapper代理失败,现在钻研少了,不喜欢看未知的错误了,立即改正.错误打印说mapper.xml已经注册,仔细查看SQLMapConfig.xml发现 ...
- 【应用篇】MyBatis学习笔记
MyBatis学习笔记 一 环境配置 1 什么是MyBatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的 JDBC 代码和参 ...
- 【Mybatis】Mybatis学习由浅入深(二)
前言 上篇介绍到了Mybatis的优缺点,这篇接下来介绍一下流程情况和配置信息. MyBatis简介 Mybatis工作流程 加载配置信息初始化 通过配置文件或注解将配置信息加载成Statemen ...
- Mybatis学习(狂神)
文章目录 前言 1. Mybatis简介 1.1.什么是MyBatis 1.2 .持久化 1.3.持久层 1.4.为什么需要Mybatis 2.MyBatis第一个程序 2.1.源码演示 2.2.可能 ...
最新文章
- Chrome Restful Api 测试工具 Postman-REST-Client离线安装包下载,Axure RP Extension for Chrome离线版下载...
- i3处理器_十代酷睿i3-10100F配GTX1650S组装机配置清单
- 同步 异步 阻塞 非阻塞概念区分
- Swift之深入解析构造过程和析构过程
- BellmanFord
- DOM-7 【兼容】事件处理函数、冒泡捕获、阻止冒泡默认事件
- python内置对象是什么_Python的内置对象类型——元组、文件,python
- 在附近小程序中展示你的小程序-微信小程序开发-视频教程21
- 利用Java实现从键盘输入一个double型数,输出该数的整数部分和小数部分
- hdu 4043 概率
- 把PDF或图片的数据导入到SPSS中的方法
- 西瓜卡顿 ANR 优化治理及监控体系建设
- 使用hifiasm组装hifi基因组的方法介绍
- 【机器学习】手写识别系统
- 数据的离散程度度量:极差、四分位差、平均差、方差、标准差、异众比率、离散系数
- 剑三服务器维护是因为人太多吗,【剑网三相关 】阵营对战 从上午10点一直打到第二天凌晨6点半服务器维护 而且今天下午他们又开始打群架了...
- 微信小程序添加音乐组件
- 大连托福考试百家外语托福阅读考试形式怎么备考托福阅读考试
- 向下整取(Floor)和向上整取(Ceiling)
- WinForm使用百度AI识别文字(OCR)