MyBatis第二讲学习笔记 ,使用MyBatis对表执行增删改查操作——基于注解的实现
来源:互联网 发布:mac 快捷键 编辑:程序博客网 时间:2024/05/22 10:24
使用MyBatis对表执行CRUD操作——基于注解的实现
package me.gacl.mapping;
import java.util.List;
import me.gacl.domain.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
/*
*定义sql语句映射的接口,使用注解指明方法要执行的SQL的语句
*
*/
public interface UserMapperInterface {
//使用@Insert注解指明add方法要执行的SQL语句
@Insert("insert into users(name,age) values(#{name},#{age})")
public int add(User user);
//使用@Delete注解指明deleteById方法要执行的SQL
@Delete("delete from users where id = #{id}")
public int deleteById(int id);
//使用@Update注解指明update方法要执行的SQL
@Update("update users set name=#{name},age=#{age} where id=#{id}")
public int update(User user);
//使用@Select注解指明getById方法要执行的SQL
@Select("select * from users where id = #{id}")
public User getById(int id);
//使用@Select注解指明getAll方法要执行的SQL
@Select("select * from users")
public List<User> getAll();
}
注意:我们不需要针对UserMapperI接口去编写具体的实现类代码,这个具体的实现类由MyBatis帮我们动态构建出来,我们只需要直接拿来使用即可。
核心代码: UserMapperInterface mapper = sqlSession.getMapper(UserMapperI.class);//myBatis根据参数:接口的字节码文件,动态生成实现该接口的实现类
conf.xml配置文件中搭建环境的<environments>下面的<mappers>标签中注册映射接口 <mapper class="me.gacl.mapping.UserMapperI"/>,团队协作把class改为resource=某配置文件
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false" />
<property name="username" value="root" />
<property name="password" value="3333" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 注册userMapper.xml文件,
userMapper.xml位于mapping这个包下,所以resource写成mapping/userMapper.xml-->
<mapper resource="me/gacl/mapping/userMapper.xml"/>
<mapper class="mapping.UserMapperInterface"/>
</mappers>
</configuration>
import java.util.List;
import me.gacl.domain.User;
import me.gacl.mapping.UserMapperI;
import me.gacl.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
public class serviceByAnnotation {
//首先通过工具包拿到SqlSession,就是通过读取配置文件生成工厂拿到这个SqlSession的对象,它才是跟数据库打交道的对象
private SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
//核心语句:这个具体的实现类由MyBatis帮我们动态构建出来,我们只需要直接拿来使用即可。它直接实现了接口中所有的方法,方法执行的SQL语句已经写在了注解中
private UserMapperInterface userMapper = sqlSession.getMapper(UserMapperI.class);
//也就是说,现在的userMapper已经是一个接口的实现类了
public int addUser(User user){
return userMapper.add(user);
}
public int deleteById(int id){
return userMapper.deleteById(id);
}
public int update(User user){
return userMapper.update(user);
}
public User selete(int id){
return userMapper.getById(id);
}
public List<User> getAll(){
return userMapper.getAll();
}
//主函数用于测试,懒得创建测试包
public static void main(String[] args) {
serviceByAnnotation service = new serviceByAnnotation();//创建服务对象
// User user = new User("学霸大宇",21);
// int i =service.addUser(user);
//// System.out.println(i);
// int i = service.update(new User(8,"注释",170608));
// System.out.println(i);
// User user = service.selete(1);
// System.out.println(user);
List<User> list = service.getAll();
for(User user : list){
System.out.println(user);
}
}
}
package me.gacl.mapping;
import java.util.List;
import me.gacl.domain.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
/*
*定义sql语句映射的接口,使用注解指明方法要执行的SQL的语句
*
*/
public interface UserMapperInterface {
//使用@Insert注解指明add方法要执行的SQL语句
@Insert("insert into users(name,age) values(#{name},#{age})")
public int add(User user);
//使用@Delete注解指明deleteById方法要执行的SQL
@Delete("delete from users where id = #{id}")
public int deleteById(int id);
//使用@Update注解指明update方法要执行的SQL
@Update("update users set name=#{name},age=#{age} where id=#{id}")
public int update(User user);
//使用@Select注解指明getById方法要执行的SQL
@Select("select * from users where id = #{id}")
public User getById(int id);
//使用@Select注解指明getAll方法要执行的SQL
@Select("select * from users")
public List<User> getAll();
}
注意:我们不需要针对UserMapperI接口去编写具体的实现类代码,这个具体的实现类由MyBatis帮我们动态构建出来,我们只需要直接拿来使用即可。
核心代码: UserMapperInterface mapper = sqlSession.getMapper(UserMapperI.class);//myBatis根据参数:接口的字节码文件,动态生成实现该接口的实现类
conf.xml配置文件中搭建环境的<environments>下面的<mappers>标签中注册映射接口 <mapper class="me.gacl.mapping.UserMapperI"/>,团队协作把class改为resource=某配置文件
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false" />
<property name="username" value="root" />
<property name="password" value="3333" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 注册userMapper.xml文件,
userMapper.xml位于mapping这个包下,所以resource写成mapping/userMapper.xml-->
<mapper resource="me/gacl/mapping/userMapper.xml"/>
<mapper class="mapping.UserMapperInterface"/>
</mappers>
</configuration>
封装了提供了服务service代码
package me.gacl.service;
import java.util.List;
import me.gacl.domain.User;
import me.gacl.mapping.UserMapperI;
import me.gacl.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
public class serviceByAnnotation {
//首先通过工具包拿到SqlSession,就是通过读取配置文件生成工厂拿到这个SqlSession的对象,它才是跟数据库打交道的对象
private SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
//核心语句:这个具体的实现类由MyBatis帮我们动态构建出来,我们只需要直接拿来使用即可。它直接实现了接口中所有的方法,方法执行的SQL语句已经写在了注解中
private UserMapperInterface userMapper = sqlSession.getMapper(UserMapperI.class);
//也就是说,现在的userMapper已经是一个接口的实现类了
public int addUser(User user){
return userMapper.add(user);
}
public int deleteById(int id){
return userMapper.deleteById(id);
}
public int update(User user){
return userMapper.update(user);
}
public User selete(int id){
return userMapper.getById(id);
}
public List<User> getAll(){
return userMapper.getAll();
}
//主函数用于测试,懒得创建测试包
public static void main(String[] args) {
serviceByAnnotation service = new serviceByAnnotation();//创建服务对象
// User user = new User("学霸大宇",21);
// int i =service.addUser(user);
//// System.out.println(i);
// int i = service.update(new User(8,"注释",170608));
// System.out.println(i);
// User user = service.selete(1);
// System.out.println(user);
List<User> list = service.getAll();
for(User user : list){
System.out.println(user);
}
}
}
阅读全文
0 0
- MyBatis第二讲学习笔记 ,使用MyBatis对表执行增删改查操作——基于注解的实现
- 使用MyBatis对表执行增删改查操作——基于注解的实现
- MyBatis -- 对表进行增删改查(基于注解的实现)
- Mybatis-基于注解的增删改查
- MyBatis使用注解实现增删改查
- 使用MyBatis对表执行CRUD操作——基于注解的实现
- MyBatis对表执行CRUD(增删改查)操作
- 使用MyBatis进行对数据表的增删改查操作
- Mybatis(一)—实现对数据库的增删改查操作
- MyBatis(四)增删改查--基于注解
- MyBatis -- 对表进行增删改查(基于XML的实现)
- Mybatis框架基于注解的方式,实对数据现增删改查
- Mybatis框架基于注解的方式,实对数据现增删改查
- mybatis利用注解实现增删改查
- Mybatis 注解实例实现增删改查
- mybatis学习入门使用注解增删改查(二)
- Mybatis入门学习篇(二)之基于注解的增删改查
- Mybatis学习使用(一) —— 增删查改
- springMVC拦截器进行登录验证
- gsoap编译
- JNI/NDK开发指南(六)——C/C++访问Java实例方法和静态方法
- step 0.1
- 我是来立flag的
- MyBatis第二讲学习笔记 ,使用MyBatis对表执行增删改查操作——基于注解的实现
- JNI/NDK开发指南(七)——C/C++访问Java实例变量和静态变量
- ios oc中的静态方法和实例方法、单例模式
- LeetCode 67. Add Binary
- Linux(CentOS)搭建redmine项目管理系统
- QT编程--用户注册程序的设计与实现
- vue2.0构建单页应用最佳实战 (一)
- 第三十三天:js写的一个计算器
- JNI/NDK开发指南(八)——调用构造方法和父类实例方法