mybatis模板

因为这里是说mybatis的,所以呢 servlet就不做多说了,代码也不在这里贴出来了.
log4j.properties
log4j.rootLogger=DEBUG,Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.org.apache=INFO
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>
<!-- 注意:一定要加在<properties>之后且<typeAliases>之前 -->
<settings>
<setting name="logImpl" value="LOG4J" />
</settings>
<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://127.0.0.1:3306/micro_message" />
<property name="username" value="root" />
<property name="password" value="gys" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/imooc/config/sqlxml/message.xml" />
</mappers>
</configuration>
message.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="message">
<resultMap type="com.imooc.bean.Message" id="MessageResult">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="COMMAND" jdbcType="VARCHAR" property="command" />
<result column="DESCRIPTION" jdbcType="VARCHAR" property="description" />
<result column="CONTENT" jdbcType="VARCHAR" property="content" />
</resultMap> <select id="queryMessageList" parameterType="com.imooc.bean.Message" resultMap="MessageResult">
select ID,COMMAND,DESCRIPTION,CONTENT from message
<where>
<!--以下条件 test="command != null && "".equals(command.trim()) " -->
<!-- <if test="command !=null && !"".equals(command.trim())">
and COMMAND =#{command}
</if> -->
<if test="command != null and !"".equals(command.trim())">
and command=#{command}
</if>
<if test="description != null and !"".equals(description.trim())">
and description like '%' #{description} '%'
</if>
</where>
</select>
<!-- 单个删除 ,这种情况 参数用_parameter -->
<delete id="deleteOne" parameterType="int">
delete from message where ID=#{_parameter}
</delete> <!-- 批量删除 -->
<delete id="deleteBatch" parameterType="java.util.List">
delete from message where ID in (
<foreach collection="list" item="item" separator=",">
#{item}
</foreach>
)
</delete> <!-- 这里的keyProperty对应的是类中的属性,不是数据库中的字段,插入数据返回id主键值 -->
<insert id="insertData" parameterType="com.imooc.bean.Message" useGeneratedKeys="true" keyProperty="id">
insert into message
(COMMAND,DESCRIPTION,CONTENT)
values
(#{command},#{description},#{content})
</insert> <select id="getDataByCommand" parameterType="String" resultMap="MessageResult" resultType="com.imooc.bean.Message">
select ID,COMMAND,DESCRIPTION,CONTENT from message where COMMAND=#{_parameter} limit 1
</select> </mapper>
DBAccess.java
package com.imooc.db; import java.io.IOException;
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; /**
* 访问数据库
* @author gys
*
*/
public class DBAccess {
public SqlSession getSqlSession() throws IOException{
//通过配置文件获取数据库连接信息
Reader reader= Resources.getResourceAsReader("com/imooc/config/mybatis-config.xml");
//通过配置信息构建一个SqlSessionFactory
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
//通过sqlSessionFactory打开一个数据库回话
SqlSession sqlSession=sqlSessionFactory.openSession();
return sqlSession;
}
}
MessageDao.java
package com.imooc.dao; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import org.apache.ibatis.session.SqlSession; import com.imooc.bean.Message;
import com.imooc.db.DBAccess; /**
* 和message表相关的数据库操作
* @author gys
*
*/
public class MessageDao {
public List<Message> queryMessageList(String command,String description){
List<Message> messageList=new ArrayList<Message>();
DBAccess dbAccess=new DBAccess();
SqlSession sqlSession=null;
try {
sqlSession= dbAccess.getSqlSession();
Message message=new Message();
message.setCommand(command);
message.setDescription(description); //通过sqlSession执行sql语句
messageList=sqlSession.selectList("message.queryMessageList",message);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(sqlSession !=null){
sqlSession.close();
}
}
return messageList;
}
/**
* 单条删除
* @param id
*/
public void deleteOne(int id){
DBAccess dbAccess=new DBAccess();
SqlSession sqlSession=null;
try {
sqlSession= dbAccess.getSqlSession();
//通过sqlSession执行sql语句
int count=sqlSession.delete("message.deleteOne",id);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(sqlSession !=null){
sqlSession.close();
}
}
} /**
* 批量删除
*/
public void deleteBatch(List<Integer> ids){
DBAccess dbAccess=new DBAccess();
SqlSession sqlSession=null;
try {
sqlSession= dbAccess.getSqlSession();
//通过sqlSession执行sql语句
int count=sqlSession.delete("message.deleteBatch",ids);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(sqlSession !=null){
sqlSession.close();
}
}
} /**
* 插入数据,获取主键
*/
public int insertData(Message message){
DBAccess dbAccess=new DBAccess();
SqlSession sqlSession=null;
int id=0;
try {
sqlSession= dbAccess.getSqlSession();
//通过sqlSession执行sql语句
int count=sqlSession.insert("message.insertData",message);//返回受影响的行数
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(sqlSession !=null){
sqlSession.close();
}
}
id=message.getId();
return id;//返回插入数据的id
} /**
* 获取单个数据
*/
public Message getDataByCommand(String command){
DBAccess dbAccess=new DBAccess();
SqlSession sqlSession=null;
Message message=null;
try {
sqlSession= dbAccess.getSqlSession();
//通过sqlSession执行sql语句
message=sqlSession.selectOne("message.getDataByCommand",command);//返回受影响的行数
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(sqlSession !=null){
sqlSession.close();
}
}
return message;//返回插入数据的id
} }
MessageService.java
package com.imooc.service; import java.util.ArrayList;
import java.util.List; import com.imooc.bean.Message;
import com.imooc.dao.MessageDao; public class MessageService {
public List<Message> queryMessageList(String command,String description){
MessageDao messageDao=new MessageDao();
return messageDao.queryMessageList(command, description);
} //单条删除
public void deleteOne(String id){
if(id!=null && !"".equals(id.trim())){
MessageDao messageDao=new MessageDao();
messageDao.deleteOne(Integer.valueOf(id));
}
} /**
* 批量删除
*/
public void deleteBatch(String[] ids){
MessageDao messageDao=new MessageDao();
List<Integer> list=new ArrayList<Integer>();
if(ids==null){
System.out.println("请选择删除的项目!");
}else{
for(String id:ids){
list.add(Integer.valueOf(id));
}
messageDao.deleteBatch(list);
} }
/**
* 批量删除
*/
public int insertData(Message message){
MessageDao messageDao=new MessageDao();
int id=messageDao.insertData(message);
return id;
} /**
* 获取单个数据
*/
public Message getDataByCommand(String command){
MessageDao messageDao=new MessageDao();
return messageDao.getDataByCommand(command);
} }
这些就是mybatis最基础的模板代码.供以后查找用
mybatis模板的更多相关文章
- MyBatis 模板
mybatis-config.xml: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE co ...
- Spring mybatis源码篇章-MybatisDAO文件解析(一)
前言:通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-SqlSessionFactory 加载指定的mybatis主文件 Mybatis模板文件,其中的属性 ...
- springboot mybatis 多数据源配置
首先导入mybatis等包,这里就不多说. 下面是配置多数据源和mybatis,每个数据源对应一套mybatis模板 数据源1: package com.aaaaaaa.config.datasour ...
- SpringBoot+SpringCloud+vue+Element开发项目——集成MyBatis框架
添加mybatis-spring-boot-starter依赖 pox.xml <!--mybatis--> <dependency> <groupId>org.m ...
- Spring mybatis源码篇章-Mybatis主文件加载
通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-SqlSessionFactory 前话 本文承接前文的内容继续往下扩展,通过Spring与Mybatis的 ...
- Mybatis总结一之Mybatis项目的创建
一.mybatis概念 Mybatis是对象和表之间映射关系的持久层框架. 二.Mybatis的导入与创建 第一步,创建web项目,引入mybatis依赖的jar包----mybatis-3.4.6. ...
- Thymeleaf+SpringBoot+Mybatis实现的齐贤易游网旅游信息管理系统
项目简介 项目来源于:https://github.com/liuyongfei-1998/root 本系统是基于Thymeleaf+SpringBoot+Mybatis.是非常标准的SSM三大框架( ...
- spring boot集成mybatis框架
概述 中文官网:http://www.mybatis.cn 参考教程:https://www.w3cschool.cn/mybatis MyBatis Plus:http://mp.baomidou. ...
- Spring从认识到细化了解
目录 Spring的介绍 基本运行环境搭建 IoC 介绍: 示例使用: 使用说明: 使用注意: Bean的实例化方式 Bean的作用范围的配置: 补充: DI: 属性注入: 补充: IoC的注解方式: ...
随机推荐
- Oracle数据库——SQL高级查询
一.涉及内容 1.掌握SELECT语句的多表连接查询. 2.掌握SELECT语句的子查询. 二.具体操作 (一)根据Oracle数据库scott方案下的emp表和dept表,完成下列操作: 1.查询所 ...
- windows中用eclipse开发Android环境搭建SDK安装异常
SDK更新时的“https://dl-ssl.google.com refused”错误 Download interrupted: hostname in certificate didn't ma ...
- String equals的技巧
把常量放到前面,可以避免null指针问题 System.out.print("".equals(null)); String abc = null; System.out.prin ...
- R(八): R分词统计-老九门
分析文本内容基本的步骤:提取文本中的词语 -> 统计词语频率 -> 词频属性可视化.词频:能反映词语在文本中的重要性,一般越重要的词语,在文本中出现的次数就会越多.词云:让词语的频率属性可 ...
- EnterpriseLibrary之Caching应用
http://blog.csdn.net/linux7985/article/details/6239433 http://cache.baiducontent.com/c?m=9f65cb4a8c8 ...
- 【Flex学习】Flex4学习网站
http://blog.minidx.com/category/flex 来自为知笔记(Wiz)
- Add LUN to ASM in Linux
# Create new LUN for Linux in the AMS2100 # echo "- - -" >/sys/class/scsi_host/host3/sc ...
- android studio使用发布者证书调试
某些时候还是要用到的,直接说步骤,修改app.gradle apply plugin: 'com.android.application' android { .................... ...
- 基于session的简易购物车引发的问题
一.功能描述: 页面如下所示: 运行报错: java.io.FileNotFoundException: E:\apache-tomcat-8.0.37\work\Catalina\localhos ...
- 工作中Linux常用命令
rpm -qa|grep -i mysql rpm -ev mysql-server-5.1.73-5.el6_6.x86_64 如果报: error: Failed dependencies: li ...