<select… />元素通常用于定义一条Select语句。需要指定如下两个属性:
- id :SQL语句的唯一标识。需要与Mapper接口的方法名相同。
- resultType或resultMap: resultType指定结果集每行记录映射对象类型(同名映射)。
resultMap需要定义专门的映射规则。
如果你用@Select注解,id不需要(直接修饰Mapper接口的方法)。
resultType也不需要(MyBatis可通过方法返回值类型来推断出每行记录映射对象类型)
resultMap - 对应@Results注解。
<select…/>元素还支持大量的额外属性,如果使用@Select注解,需要配置@Options注解来指定这些额外的属性。
主类
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映射文件。
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 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>