mybatis入门篇:Mybatis注解方式的基本用法
@Select
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>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="useGeneratedKeys" value="true"/>
</settings>
<typeAliases>
<package name="com.forest.owl.entity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value=""/>
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/forest?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.forest.owl.mapper"/>
</mappers>
</configuration>
2、编写UserMapper接口
package com.forest.owl.mapper;
import com.forest.owl.entity.User;
import org.apache.ibatis.annotations.Select;
public interface UserMapper {
@Select("SELECT * FROM user WHERE id=#{id}")
User selectUserById(Long id);
}
3、测试
@Test
public void UserMapperTest(){
SqlSession sqlSession = getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.selectUserById((long) 2);
System.out.println(user.getUserName());
System.out.println(user.getHeadImg());
System.out.println(user.getCreateTime());
}
4、关联查询
@Results({
@Result(property = "id", column = "id"),
@Result(property = "userName", column = "user_name"),
@Result(property = "userPassword", column = "user_password"),
@Result(property = "userPhone", column = "user_phone"),
@Result(property = "userEmail", column = "user_email"),
@Result(property = "headImg", column = "head_img"),
@Result(property = "createTime", column = "create_time"),
@Result(property = "updateTime", column = "update_time"),
@Result(property = "role.roleName", column = "role_name")
})
@Select("SELECT u.* ,r.role_name FROM user u INNER JOIN user_role ur on ur.user_id=u.id INNER JOIN role r on r.id=ur.role_id " +
"WHERE u.id=#{id}")
List<User> selectUserById(Long id);
关于注解形式的查询,也可以通过Provider进行sql的动态生成。如:
新建UserProvider类:
package com.forest.owl.provider;
import org.apache.ibatis.jdbc.SQL;
public class UserProvider {
public String selectUserById(Long id){
return new SQL(){
{
SELECT("u.* ,r.role_name");
FROM("user u");
LEFT_OUTER_JOIN("user_role ur on ur.user_id=u.id");
LEFT_OUTER_JOIN("role r on r.id=ur.role_id");
WHERE("u.id=#{id}");
}
}.toString();
}
}
此时UserMapper可以这么写:
public interface UserMapper {
@Results({
@Result(property = "id", column = "id"),
@Result(property = "userName", column = "user_name"),
@Result(property = "userPassword", column = "user_password"),
@Result(property = "userPhone", column = "user_phone"),
@Result(property = "userEmail", column = "user_email"),
@Result(property = "headImg", column = "head_img"),
@Result(property = "createTime", column = "create_time"),
@Result(property = "updateTime", column = "update_time"),
@Result(property = "role.roleName", column = "role_name")
})
@SelectProvider(type = UserProvider.class, method = "selectUserById")
List<User> selectUserById(Long id);
}
@insert
这里介绍普通插入和返回主键方式的插入
普通插入数据
package com.forest.owl.mapper;
import com.forest.owl.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
public interface UserMapper {
@Insert("INSERT INTO user (user_name, user_password, user_phone, user_email, head_img, create_time, update_time) VALUES (#{userName}, #{userPassword}, #{userPhone}, #{userEmail}, #{headImg}, #{createTime}, #{updateTime})")int insertUser(User user);
}
回写主键方式的插入(注意:哪怕mybatis-config.xml配置中已经配置了useGeneratedKeys,在@Options中仍旧需要再配置一次)
package com.forest.owl.mapper;
import com.forest.owl.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
public interface UserMapper {
@Insert("INSERT INTO user (user_name, user_password, user_phone, user_email, head_img, create_time, update_time) VALUES (#{userName}, #{userPassword}, #{userPhone}, #{userEmail}, #{headImg}, #{createTime}, #{updateTime})")
@Options(useGeneratedKeys = true, keyProperty = "id")
int insertUser(User user);
}
mybatis入门篇:Mybatis注解方式的基本用法的更多相关文章
- Spring Boot入门(六):使用MyBatis访问MySql数据库(注解方式)
本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 本篇博客我们讲解下在Spring Boot中使用MyBatis访问MySql数据库的简单用法. 1.前期 ...
- SpringMVC入门(基于注解方式实现)
---------------------siwuxie095 SpringMVC 入门(基于注解方式实现) SpringMVC ...
- Python入门篇-类型注解
Python入门篇-类型注解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.函数定义的弊端 1>.动态语言很灵活,但是这种特性也是弊端 Python是动态语言,变量随时可 ...
- MyBatis从入门到精通(五):MyBatis 注解方式的基本用法
最近在读刘增辉老师所著的<MyBatis从入门到精通>一书,很有收获,于是将自己学习的过程以博客形式输出,如有错误,欢迎指正,如帮助到你,不胜荣幸! 1. @Select 注解 1.1 使 ...
- Mybatis入门篇之结果映射,你射准了吗?
目录 前言 什么是结果映射? 如何映射? 别名映射 驼峰映射 配置文件开启驼峰映射 配置类中开启驼峰映射 resultMap映射 总结 高级结果映射 关联(association) 例子 关联的嵌套 ...
- 从零开始学JAVA(09)-使用SpringMVC4 + Mybatis + MySql 例子(注解方式开发)
项目需要,继续学习springmvc,这里加入Mybatis对数据库的访问,并写下一个简单的例子便于以后学习,希望对看的人有帮助.上一篇被移出博客主页,这一篇努力排版整齐,更原创,希望不要再被移出主页 ...
- MyBatis入门篇
一.什么是MyBatis MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改 ...
- springboot整合mybatis完整示例, mapper注解方式和xml配置文件方式实现(我们要优雅地编程)
一.注解方式 pom <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId& ...
- mybatis入门篇:代码生成器(MyBatis Generator)
这篇文章只是按照自己的需要去配置代码生成器,未对所有配置进行讲解,需要了解具体详情的,请到官网查阅文档.传送门:http://www.mybatis.org/generator/ 1.首先引入相关的依 ...
- mybatis入门篇:通过SqlSession.selectList进行数据查询
作为一个java菜鸟,早就从慕课网中学到一些基本的mybatis的用法,但是一直不成体系,懵懵懂懂,既然正式入了java这个坑,就打算好好学学,所以买了本<MyBatis从入门到精通>,在 ...
随机推荐
- JavaSE-运算符和流程控制
一.运算符 1.算术运算符: (1) 进行混合运算的时候:byte,short,char之间不会相互转换,都会自动提升为int类型 .其他类型进行混合运算的时候都是小的数据类型提升为大的:byte,s ...
- phpStudy 切换版本后没有权限的问题
在httpd-vhosts.conf配置如下: <VirtualHost *:80> ServerName www.jy.com DocumentRoot "C:\htdocs\ ...
- ORB_SLAM2安装
进入工程目录,我们发现有两个sh文件,一个是build.sh另一个是build_ros.sh. 这两个都可以进行ORB_SLAM2的安装,我们先来看一下build.sh echo "Conf ...
- Vue慕课网音乐项目随手记--node代理及数据抓取
1.抓取数据 链接 https://y.qq.com/portal/playlist.html Parameters 通过上图能看到,qq音乐通过设置了refer和host来保护接口. 那么怎么才 ...
- 评测parser的好坏
1.在dependency parsing中一般是用 LAS UAS 来衡量 简要说来UAS是知道是边对了(也就是它依赖的节点找对了)就算对,而LAS在前者的基础上要求更加严格,还要求边的Label也 ...
- GitHub 优秀的项目地址
Type LibDescription LibLocation Android SlidingMenu https://github.com/jfeinstein10/SlidingMenu ...
- linux安装mysql后root无法登录 sql 无法登录
linux安装mysql后root无法登录 问题:[root@localhost mysql]# mysql -u root -pEnter password: ERROR 1045 (28000): ...
- Linux安装JDK.8
E&T: CentOS_7.4 64位; jdk-8u191-linux-x64; Xftp5; Xshell5; P1.下载Linux环境下的jdk1.8 下载链接: https://www ...
- lua 5.3最简单plugin编写
#include <windows.h> #include "lauxlib.h" /* Pop-up a Windows message box with your ...
- 如何长期试用Beyond Compare 4
打开Beyond Compare 4,发现已经过了试用期 我们可以点击立即购买,购买相关的Beyond Compare 4产品,如果你已经有密钥了,可以选择使用密钥 如果还想继续试用,则找到自 ...