mybatis配置开发
以mysql为例:
- <?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 resource="db.properties"/>
- <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/mydata" />
- <property name="username" value="root" />
- <property name="password" value="tian" /> -->
- <property name="driver" value="${driver}" />
<!--通过db.properties文件进行配置--> - <property name="url" value="${url}" />
- <property name="username" value="${name}" />
- <property name="password" value="${password}" />
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <!-- 注册userMapper.xml文件,
- userMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/userMapper.xml-->
- <mapper resource="infomap.xml"/>
- <mapper class="mybatis.BatisInfo"></mapper>
- </mappers>
- </configuration>
- driver=com.mysql.jdbc.Driver
- url=jdbc:mysql://localhost:3306/mydata
- name=root
- password=tian
- package mybatis
- public class Info {
- private int id;
- private String name;
- private String password;
- public void setId(int id)
- {
- this.id=id;
- }
- public int getId()
- {
- return id;
- }
- public void setName(String name)
- {
- this.name=name;
- }
- public String getName()
- {
- return name;
- }
- public void setPassword(String password)
- {
- this.password=password;
- }
- public String getPassword()
- {
- return password;
- }
- public String toString()
- {
- return "name"+name+";password"+password;
- }
- }
- <?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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
- 例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)
- -->
- <mapper namespace="me.gacl.mapping.userMapper">
- <!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,
- id属性值必须是唯一的,不能够重复使用parameterType属性指明查询时使用的参数类型,
- resultType属性指明查询返回的结果集类型resultType="me.gacl.domain.User"
- 就表示将查询结果封装成一个User类的对象返回User类就是users表所对应的实体类-->
- <!--根据id查询得到一个user对象-->
- <select id="getUser" parameterType="int"
- resultType="mybatis.Info">
- select * from info where id=#{id}
- </select>
- <insert id="addUser" parameterType="mybatis.Info">
- insert into info (name, password) values (#{name},#{password})
- </insert>
- <delete id="deleteUser" parameterType="int">
- delete from info where id=#{id}
- </delete>
- <update id="updateInfo" parameterType="mybatis.Info" >
- update info set name=#{name},password=#{password} where id=#{id}
- </update>
- <select id="getAllUsers" resultType="mybatis.Info">
- select * from info
- </select>
- </mapper>
- @Test
- public void testAdd()
- {
- SqlSession sqlSession=BatisSession.getSqlSession(true);
- String statement="me.gacl.mapping.userMapper.addUser"; //xml中的namespace+id
- Info info=new Info();
- info.setName("tian");
- info.setPassword("tian");
- int result=sqlSession.insert(statement, info); //sql语句+参数,最后一个返回值
- sqlSession.close();
- System.out.println(result+"sjdjsjd");
- }
- @Test
- public void testDelete()
- {
- SqlSession sqlSession=BatisSession.getSqlSession(true);
- String statement="me.gacl.mapping.userMapper.deleteUser";
- int result=sqlSession.delete(statement,2);
- sqlSession.close();
- System.out.println(result);
- }
- @Test
- public void getAll()
- {
- SqlSession sqlSession=BatisSession.getSqlSession(true);
- String statement="me.gacl.mapping.userMapper.getAllUsers";
- List<Info> list=sqlSession.selectList(statement);
- sqlSession.close();
- System.out.println(list);
- }
- public interface BatisInfo {
- //包括一个sql 语句,一个函数及函数的参数,函数返回值
- @Insert("insert into info(name,password) values(#{name},#{password})")
- public int add(Info info);
- @Delete("delete from info where id=#{id}")
- public int delete(int id);
- @Select("select * from info where id=#{id}")
- public Info getById(int id);
- @Select("select * from info")
- public List<Info> getAll();
- @Update("update info set name=#{name},password=#{password} where id=#{id}")
- public int update(Info info);
- }
- @Test
- public void testAdd()
- {
- SqlSession sqlSession=BatisSession.getSqlSession(true);
- BatisInfo batisInfo=sqlSession.getMapper(BatisInfo.class); //获得接口类
- Info info=new Info();
- info.setName("annotion");
- info.setPassword("annotion");
- int result=batisInfo.add(info);//调用函数,传入参数,获得操作结果
- sqlSession.close();
- System.out.println(result);
- }
- @Test
- public void testDelete()
- {
- SqlSession sqlSession=BatisSession.getSqlSession(true);
- BatisInfo batisInfo=sqlSession.getMapper(BatisInfo.class);
- int result=batisInfo.delete(4);
- sqlSession.close();
- System.out.println(result);
- }
- @Test
- public void testUpdate()
- {
- SqlSession sqlSession=BatisSession.getSqlSession(true);
- BatisInfo batisInfo=sqlSession.getMapper(BatisInfo.class);
- Info info=new Info();
- info.setName("annotiontest");
- info.setPassword("annotiontest");
- int result=batisInfo.update(info);
- sqlSession.close();
- System.out.println(result);
- }
- @Test
- public void testGetAll()
- {
- SqlSession sqlSession=BatisSession.getSqlSession(true);
- BatisInfo batisInfo=sqlSession.getMapper(BatisInfo.class);
- List<Info> list=batisInfo.getAll();
- sqlSession.close();
- System.out.println(list);
- }
mybatis配置开发的更多相关文章
- 【Java】一次SpringMVC+ Mybatis 配置多数据源经历
需求 现在在维护的是学校的一款信息服务APP的后台,最近要开发一些新功能,其中一个就是加入学校电影院的在线购票.在线购票实际上已经有一套系统了,但是是外包给别人开发的,我们拿不到代码只能拿到数据库,并 ...
- SpringMVC,Spring,Hibernate,Mybatis架构开发搭建之SpringMVC部分
SpringMVC,Spring,Hibernate,Mybatis架构开发搭建之SpringMVC部分 辞职待业青年就是有很多时间来写博客,以前在传统行业技术强度相对不大,不处理大数据,也不弄高并发 ...
- 框架篇:Spring+SpringMVC+Mybatis整合开发
前言: 前面我已搭建过ssh框架(http://www.cnblogs.com/xrog/p/6359706.html),然而mybatis表示不服啊. Mybatis:"我抗议!" ...
- Mybatis配置信息浅析 MyBatis简介(二)
官方文档入门篇中有明确说明 每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为中心的. SqlSessionFactory 的实例可以通过 SqlSessionF ...
- 【转】一次SpringMVC+ Mybatis 配置多数据源经历
需求 现在在维护的是学校的一款信息服务APP的后台,最近要开发一些新功能,其中一个就是加入学校电影院的在线购票.在线购票实际上已经有一套系统了,但是是外包给别人开发的,我们拿不到代码只能拿到数据库,并 ...
- idea spring-boot gradle mybatis 搭建开发环境
使用工具idea 2017.2开发,gradle构建项目,使用的技术有spring-boot.mybatis 1.新建项目 说明:1.src为源码路径,开发主要在src下 2.src/main/jav ...
- Spring Boot 源码分析 数据源 + Mybatis 配置
公司今年开始使用 Spring Boot 开发,当然使用 Spring Boot 也是大势所趋,尤其是现在微服务的趋向,当然是选择基于Spring Boot 的 Spring Cloud.(所谓的 S ...
- Springboot 和 Mybatis集成开发
Springboot 和 Mybatis集成开发 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 jdk:1.7.0_79 maven:3.3.9 额外功能 PageHel ...
- SpringBoot整合mybatis——配置mybatis驼峰命名规则自动转换
一.简述 mybatis驼峰式命名规则自动转换: 使用前提:数据库表设计按照规范“字段名中各单词使用下划线"_"划分”: 使用好处:省去mapper.xml文件中繁琐编写表字段列表 ...
随机推荐
- 闪屏页面开发遇到的问题you need to use a theme.appcompat theme (or descendant)
开始做一个新闻客户端的应用,在做到闪屏页面时之前发布应用的时候总是报错,原因是我在splash.java中把Activty写成ActionBarActivity,导包,然后就可以了.以前也遇到过这种情 ...
- C++对象模型的那些事儿之四:拷贝构造函数
前言 对于一个没有实例化的空类,编译器不会给它默认生成任何函数,当实例化一个空类后,编译器会根据需要生成相应的函数.这类函数包括一下几个: 构造函数 拷贝构造函数 析构函数 赋值运算符 在上一篇博文C ...
- UNIX网络编程——通用套接字选项
1. SO_BROADCAST 套接字选项 本选项开启或禁止进程发送广播消息的能力.只有数据报套接字支持广播,并且还必须是在支持广播消息的网络上(例如以太网,令牌环网等).我们不可能在点对点链路上进行 ...
- 初探linux子系统集之timer子系统(三)
因为现在的linux虽然还是可以使用低精度的timer,但是趋势是高精度hrtimer,所以上一篇试着翻译一下hrtimer的一些介绍,翻译的不是很好,看来英语还得好好学习啊,下面还是好好学习下lin ...
- 【一天一道LeetCode】#165. Compare Version Numbers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- String压缩 解压缩
数据传输时,有时需要将数据压缩和解压缩,本例使用GZIPOutputStream/GZIPInputStream实现. 1.使用ISO-8859-1作为中介编码,可以保证准确还原数据 2.字符编码确定 ...
- 从JDK源码角度看java并发的原子性如何保证
JDK源码中,在研究AQS框架时,会发现很多地方都使用了CAS操作,在并发实现中CAS操作必须具备原子性,而且是硬件级别的原子性,java被隔离在硬件之上,明显力不从心,这时为了能直接操作操作系统层面 ...
- Weblogic10 集群配置
1.预备知识 什么是Domain和Server Domain Domain是WebLogic Server实例的基本管理单元.所谓Domain就是,由配置为Administrator Serve ...
- 谈谈Ext JS的组件——组件基类:Ext.Component
概述 Ext.Component是所有Ext组件的基类,这在Ext.Component的API中第一句话就提到了.然后第二段说明了它包含的基本功能:隐藏/显示.启用/禁用以及尺寸控制等.除了以上这些基 ...
- STL:set/multiset用法详解
集合 使用set或multiset之前,必须加入头文件<set> Set.multiset都是集合类,差别在与set中不允许有重复元素,multiset中允许有重复元素. sets和mul ...