Mybatis 33_select元素和@Select注解 项目0408@Select

33_select元素和@Select注解

select元素和@Select注解

<select... />元素通常用于定义一条Select语句。需要指定如下两个属性:

  • id :SQL语句的唯一标识。需要与Mapper接口的方法名相同。

  • resultType或resultMap: resultType指定结果集每行记录映射对象类型(同名映射)。

resultMap需要定义专门的映射规则。

如果你用@Select注解,id不需要(直接修饰Mapper接口的方法)。

resultType也不需要(MyBatis可通过方法返回值类型来推断出每行记录映射对象类型)

resultMap - 对应@Results注解。

<select.../>元素还支持大量的额外属性,如果使用@Select注解,需要配置@Options注解来指定这些额外的属性。

项目0408@Select

主类

java 复制代码
package lee;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.itcheng.app.dao.NewsMapper;
import org.itcheng.app.domain.News;

public class NewsManager
{
	// SqlSessionFactory应该是应用级别
	private static SqlSessionFactory sqlSessionFactory;
	public static void main(String[] args) throws IOException
	{
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		// 1. 创建SqlSessionFactory
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		// 2. 打开SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		
		// 保存消息
//		saveNews(sqlSession);
		/*
		 * DEBUG [main] org.itcheng.app.dao.NewsMapper.saveNews ==>  Preparing: insert into news_inf values (null, ?, ?) 
		 * DEBUG [main] org.itcheng.app.dao.NewsMapper.saveNews ==> Parameters: 测试标题(String), 测试内容(String)
		 * DEBUG [main] org.itcheng.app.dao.NewsMapper.saveNews <==    Updates: 1
		 * 
		 * */
		
		// 更新消息
//		updateNews(sqlSession);	
        /* DEBUG [main] org.itcheng.app.dao.NewsMapper.updateNews ==>  Preparing: update news_inf set news_title = ?, news_content = ? where news_id=? 
		 * DEBUG [main] org.itcheng.app.dao.NewsMapper.updateNews ==> Parameters: 修改的标题(String), 修改的内容(String), 1(Integer)
		 * DEBUG [main] org.itcheng.app.dao.NewsMapper.updateNews <==    Updates: 1
         * */
		
		// 删除消息
//		deleteNews(sqlSession);			
        /* DEBUG [main] org.itcheng.app.dao.NewsMapper.deleteNews ==>  Preparing: delete from news_inf where news_id = ? 
		 * DEBUG [main] org.itcheng.app.dao.NewsMapper.deleteNews ==> Parameters: 2(Integer)
		 * DEBUG [main] org.itcheng.app.dao.NewsMapper.deleteNews <==    Updates: 1
         * */
		
		// 查询消息
		selectNews(sqlSession);		
        /* class com.sun.proxy.$Proxy23
		 * DEBUG [main] org.itcheng.app.dao.NewsMapper.findNews ==>  Preparing: select news_id id, news_title title, news_content content from news_inf where news_id > ? 
		 * DEBUG [main] org.itcheng.app.dao.NewsMapper.findNews ==> Parameters: -1(Integer)
		 * DEBUG [main] org.itcheng.app.dao.NewsMapper.findNews <==      Total: 8
		 * [News [id=1, title=修改的标题, content=修改的内容], News [id=3, title=22, content=2222222222222], News [id=4, title=33, content=3333333333333], News [id=5, title=44, content=4444444444444], News [id=6, title=55, content=5555555555555], News [id=7, title=66, content=6666666666666], News [id=8, title=77, content=7777777777777], News [id=9, title=测试标题, content=测试内容]]
         * */
	}
	
	public static void saveNews(SqlSession sqlSession)
	{
		News news = new News(null, "测试标题", "测试内容");
		
		NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
		
		newsMapper.saveNews(news);
		
		// 4. 提交事务
		sqlSession.commit();
		// 5. 关闭资源
		sqlSession.close();	
	}
	
	public static void updateNews(SqlSession sqlSession)
	{

		News news = new News(1, "修改的标题", "修改的内容");
		
		NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
		
		newsMapper.updateNews(news);
		
		// 4. 提交事务
		sqlSession.commit();
		// 5. 关闭资源
		sqlSession.close();	
	
	}
	
	public static void deleteNews(SqlSession sqlSession)
	{
		
		// SqlSession的insert、update、delete方法都只是去执行DML语句
		// 因此这个三个方法并没有本质的区别:它们都只是调用PreparedStatement的executeUpdate()方法来执行DML语句。
		
		NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
		newsMapper.deleteNews(2);
		
		// 4. 提交事务
		sqlSession.commit();
		// 5. 关闭资源
		sqlSession.close();	
	}
	
	public static void selectNews(SqlSession sqlSession)
	{
		// 此处的NewsMapper只是一个接口
		// MyBatis会使用JDK的动态代理为Mapper接口生成实现类
		NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
		System.out.println(newsMapper.getClass());
		List<?> list = newsMapper.findNews(-1);
		
		System.out.println(list);
		
		// 4. 提交事务
		sqlSession.commit();
		// 5. 关闭资源
		sqlSession.close();	
	}	
		
}

接口类,使用注解,由于使用注解所以代替了mapper映射文件。

java 复制代码
package org.itcheng.app.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.itcheng.app.domain.News;

// Mapper组件相当于DAO组件
public interface NewsMapper
{
	@Insert("insert into news_inf values (null, #{title}, #{content})")
	int saveNews(News news);
	
	@Update("update news_inf set news_title = #{title}, news_content = #{content} " 
		 + "where news_id=#{id}")
	int updateNews(News news);
	
	@Delete("delete from news_inf where news_id = #{xyz}")
	void deleteNews(Integer id);
	
	// 当使用@Select注解时,无需指定resultType属性,因为MyBatis可根据List<News>返回值类型来推断
	@Select("select news_id id, news_title title, news_content content "
		+ "from news_inf where news_id > #{id}")
	List<News> findNews(Integer id);
}

主配置文件

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="org.itcheng.app.domain"/>
 	</typeAliases>
 	
	<!-- 用于配置多个数据库环境 -->
	<environments default="mysql">
		<!-- environment配置一个数据库环境 -->
		<environment id="mysql">
			<!-- 配置事务类型:JDBC或Managed,此时的JDBC等都是实现类的缩写 -->
			<transactionManager type="JDBC" />
			<!-- 配置数据库连接池,POOLED也是一个实现类的缩写  -->
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC" />
				<property name="username" value="root" />
				<property name="password" value="root" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<!-- mapper文件负责管理MyBatis的SQL语句 -->
		<mapper class="org.itcheng.app.dao.NewsMapper" />
	</mappers>

</configuration>
相关推荐
合作小小程序员小小店5 分钟前
web网页开发,在线%就业信息管理%系统,基于idea,html,layui,java,springboot,mysql。
java·前端·spring boot·后端·intellij-idea
陈果然DeepVersion6 分钟前
Java大厂面试真题:从Spring Boot到AI微服务的三轮技术拷问(一)
java·spring boot·redis·微服务·kafka·面试题·oauth2
晨晖214 分钟前
docker打包,启动java程序
java·docker·容器
SelectDB18 分钟前
冷查第一,再登榜首!Apache Doris 3.1 全面刷新 JSONBench 性能纪录
数据库·apache
郑州光合科技余经理22 分钟前
乡镇外卖跑腿小程序开发实战:基于PHP的乡镇同城O2O
java·开发语言·javascript·spring cloud·uni-app·php·objective-c
wei_shuo24 分钟前
智能运维×低资源占用:金仓数据库助力能源企业降本增效与国产化替换实践
运维·数据库·king base
float_六七32 分钟前
SQL中的NULL陷阱:为何=永远查不到空值
java·前端·sql
nvd1132 分钟前
GKE连接私有Cloud SQL疑难问题排查实录
数据库·sql
Dev7z34 分钟前
MySQL 错误 1046 (3D000) 是因为在执行 SQL 语句时 没有选择当前数据库
数据库·sql·mysql
漠然&&1 小时前
实战案例:用 Guava ImmutableList 优化缓存查询系统,解决多线程数据篡改与内存浪费问题
java·开发语言·缓存·guava