因为这里是说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 &amp;&amp !&quot;&quot;.equals(command.trim())">
and COMMAND =#{command}
</if> -->
<if test="command != null and !&quot;&quot;.equals(command.trim())">
and command=#{command}
</if>
<if test="description != null and !&quot;&quot;.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模板的更多相关文章

  1. MyBatis 模板

    mybatis-config.xml: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE co ...

  2. Spring mybatis源码篇章-MybatisDAO文件解析(一)

    前言:通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-SqlSessionFactory 加载指定的mybatis主文件 Mybatis模板文件,其中的属性 ...

  3. springboot mybatis 多数据源配置

    首先导入mybatis等包,这里就不多说. 下面是配置多数据源和mybatis,每个数据源对应一套mybatis模板 数据源1: package com.aaaaaaa.config.datasour ...

  4. SpringBoot+SpringCloud+vue+Element开发项目——集成MyBatis框架

    添加mybatis-spring-boot-starter依赖 pox.xml <!--mybatis--> <dependency> <groupId>org.m ...

  5. Spring mybatis源码篇章-Mybatis主文件加载

    通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-SqlSessionFactory 前话 本文承接前文的内容继续往下扩展,通过Spring与Mybatis的 ...

  6. Mybatis总结一之Mybatis项目的创建

    一.mybatis概念 Mybatis是对象和表之间映射关系的持久层框架. 二.Mybatis的导入与创建 第一步,创建web项目,引入mybatis依赖的jar包----mybatis-3.4.6. ...

  7. Thymeleaf+SpringBoot+Mybatis实现的齐贤易游网旅游信息管理系统

    项目简介 项目来源于:https://github.com/liuyongfei-1998/root 本系统是基于Thymeleaf+SpringBoot+Mybatis.是非常标准的SSM三大框架( ...

  8. spring boot集成mybatis框架

    概述 中文官网:http://www.mybatis.cn 参考教程:https://www.w3cschool.cn/mybatis MyBatis Plus:http://mp.baomidou. ...

  9. Spring从认识到细化了解

    目录 Spring的介绍 基本运行环境搭建 IoC 介绍: 示例使用: 使用说明: 使用注意: Bean的实例化方式 Bean的作用范围的配置: 补充: DI: 属性注入: 补充: IoC的注解方式: ...

随机推荐

  1. python3抓取异步百度瀑布流动态图片(一)查找post并伪装头方法

    打开流程: 用火狐打开百度图片-->打开firebug-->输入GIF图-->搜索-->点击网络-->全部 观察页面: 首先要观察的对象是“域”,图片的json一般是放在 ...

  2. 用php自带的filter函数验证、过滤数据 -转载

    PHP过滤器包含两种类型 Validation:用来验证验证项是否合法 Sanitization:用来格式化被验证的项目,因此它可能会修改验证项的值,将不合法的字符删除等. input_filters ...

  3. 如何修改Oracle字符集

    一.什么是Oracle字符集 Oracle字符集是一个字节数据的解释的符号集合,有大小之分,有相互的包容关系.ORACLE 支持国家语言的体系结构允许你使用本地化语言来存储,处理,检索数据.它使数据库 ...

  4. R提高篇(四): 数据管理二

    目录: 数学函数 统计函数 应用示例 控制流 数学函数 ceiling(x):  大于等于 x  的最小整数, 如:  ceiling(3.213)  --> 4 floor(x):     小 ...

  5. javascript取得机器名,用户名,读写注册表,启动应用程序

    javascript取得机器名,用户名,读写注册表,启动应用程序//javascript有个特殊的对象ActiveXObject,通过它可以访问windows的本地文件系统和应用程序,比如:有的时候我 ...

  6. 转载:mybatis自动生成

    MyBatis Generator中文文档 MyBatis Generator中文文档地址: http://generator.sturgeon.mopaas.com/ 该中文文档由于尽可能和原文内容 ...

  7. (转)【Android测试工具】03. ApkTool在Mac上的安装和使用(2.0版本)

    http://blog.csdn.net/wirelessqa/article/details/8997168 http://code.google.com/p/android-apktool/dow ...

  8. objective-c 随便记记

    1.tableview滚动到某一位置 [tableViewShow setContentOffset:CGPointMake(0, 0) animated:YES]; //解决tableView分割线 ...

  9. 使用 Windows PowerShell 来管理和开发 windowsazure.cn 账户的特别注意事项

    6月6日,微软面向中国大陆用户开放了Microsoft Azure公众预览版的申请界面.大家可以申请免费的 beta 试用,收到内附邀请码的通知邮件后只需输入激活码即可开始免费试用.具体网址为: ht ...

  10. 像装软件一样装系统 Win8下怎么装Win7

    像装软件一样装系统 Win8下怎么装Win7 首先,你需要一个Windows7的ISO镜像文件,非ghost版本 一般选中ISO文件,点反键在弹出菜单中以“装载”或“window资源管理器”方式打开 ...