第一节:MyBatis简介

  百度百科

第二季:Mybatis版HolleWorld实现

  例子:

  

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 resource="jdbc.properties"/> <!-- 引入jdbc.properties文件 -->
<!-- 类型别名 -->
<typeAliases>
<typeAlias alias="Student" type="com.wishwzp.model.Student"/>
</typeAliases>
<!-- 开发环境,这里就写了一个id为development的数据环境 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments> <mappers>
<mapper resource="com/wishwzp/mappers/StudentMapper.xml" />
</mappers> </configuration>

jdbc.properties:

 jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

StudentTest.java:

 package com.wishwzp.service;

 import org.apache.ibatis.session.SqlSession;

 import com.wishwzp.mappers.StudentMapper;
import com.wishwzp.model.Student;
import com.wishwzp.util.SqlSessionFactoryUtil; public class StudentTest { public static void main(String[] args) {
SqlSession sqlSession=SqlSessionFactoryUtil.openSession();
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
//do work
Student student=new Student("李四",11);
int result=studentMapper.add(student);
sqlSession.commit();
if(result>0){
System.out.println("添加成功!");
}
}
}

SqlSessionFactoryUtil.java:

 package com.wishwzp.util;

 import java.io.InputStream;

 import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class SqlSessionFactoryUtil { private static SqlSessionFactory sqlSessionFactory; public static SqlSessionFactory getSqlSessionFactory(){
if(sqlSessionFactory==null){
InputStream inputStream=null;
try{
inputStream=Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
}catch(Exception e){
e.printStackTrace();
}
}
return sqlSessionFactory;
} public static SqlSession openSession(){
return getSqlSessionFactory().openSession();
}
}

Student.java:

 package com.wishwzp.model;

 public class Student {

     private Integer id;
private String name;
private Integer age; public Student() {
super();
// TODO Auto-generated constructor stub
} public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
} public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
} }

StudentMapper.java:

 package com.wishwzp.mappers;

 import com.wishwzp.model.Student;

 public interface StudentMapper {

     public int add(Student student);
}

StudentMapper.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="com.wishwzp.mappers.StudentMapper"> <insert id="add" parameterType="Student" >
insert into t_student values(null,#{name},#{age})
</insert> </mapper>

数据库为db_mybatis:

(一)问候MyBatis3的更多相关文章

  1. SSM整合(三):Spring4与Mybatis3与SpringMVC整合

    源码下载 SSMDemo 上一节整合了Mybatis3与Spring4,接下来整合SpringMVC! 说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目! 本节全部采用 ...

  2. SSM整合(二):Spring4与Mybatis3整合

    上一节测试好了Mybatis3,接下来整合Spring4! 一.添加spring上下文配置 在src/main/resources/目录下的spring新建spring上下文配置文件applicati ...

  3. SSM集成(一):Mybatis3测试

    Spring4+Mybatis3+SpringMVC(基于注解)整合步聚: 一)Mybatis3测试; 二)Mybatis3+Spring4整合; 三)Mybatis3+Spring4+SpringM ...

  4. 基于Spring4+SpringMVC4+Mybatis3+Hibernate4+Junit4框架构建高性能企业级的部标GPS监控平台

    开发企业级的部标GPS监控平台,投入的开发力量很大,开发周期也很长,选择主流的开发语言以及成熟的开源技术框架来构建基础平台,是最恰当不过的事情,在设计之初就避免掉了技术选型的风险,避免以后在开发过程中 ...

  5. Mybatis3.x与Spring4.x整合(转)

    http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype:cre ...

  6. MyBatis入门学习教程-Mybatis3.x与Spring4.x整合

    一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype:create -DgroupId=me.gacl -DartifactId=spring4-myba ...

  7. MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合(转载)

      孤傲苍狼 只为成功找方法,不为失败找借口! MyBatis学习总结(八)--Mybatis3.x与Spring4.x整合 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: m ...

  8. 《SSM框架搭建》二.mybatis3,spring4整合

    感谢学习文章来自http://www.cnblogs.com/xdp-gacl/p/4271627.html,spring3修改为spring4.还有提示驱动过期的问题,是由于使用了mysql-con ...

  9. MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合

    一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype:create -DgroupId=me.gacl -DartifactId=spring4-myba ...

随机推荐

  1. React-Router 动画 Animation

    React-Router动画实际上和React动画没什么区别,都是使用 'react-addons-css-transition-group' 这个组件:但是,和普通的 React-Router 的 ...

  2. web项目中的执行流程参数传递详解

    还是从这个图开始讲解: struts2中有一个存放数据的中心:值栈.(值栈里面有map和对象栈) 首先:值栈的作用范围是一个请求:request作用域(一个请求是代表的一个过程,即页面点击到数据返回到 ...

  3. Package ‘RSNNS’

    0 引言 Stuttgart Neural Network Simulator(SNNS)是德国斯图加特大学开发的优秀神经网络仿真软件,为国外的神经网络研究者所广泛采用.斯图加特神经网络模拟器(SNN ...

  4. 八、java常用类

    目录 一.字符串相关类 String类 StringBuffer类 二.基本数据类型包装类 三.Math类 四.File类 五.枚举类 一.字符串相关类 1.String类 java.lang.Str ...

  5. 最新的IDEA激活方式

    IntelliJ IDEA2017.3 激活 转自:http://blog.csdn.net/zx110503/article/details/78734428 最新的IDEA激活方式 使用网上传统的 ...

  6. 转:UINavigationBar返回上一级出现nested pop animation can result in corrupted navigation bar

    [self.navigationController popViewControllerAnimated:NO]; 出现上面的错误是因为pop的时候要确保先让本页面加载完成,即如果在viewDidLo ...

  7. 前端案例分享(一):CSS+JS实现流星雨动画

    目录 引言 1.效果图 2.源码 3.案例解析 4.小问题 5.结语 引言        平常会做一些有意思的小案例练手,通常都会发到codepen上,但是codepen不能写分析.        所 ...

  8. easy-animation | Animation for Sass

    最近因为项目缘故,勾搭上了Sass. 其实在折腾Sass之前,也有简单用过一下Less.但碍于Less提供的一些API实在让人觉得有点多余,用着就是不顺手,最后就不了了之啦. Sass之所以用起来舒服 ...

  9. CodeForces 816C 思维

    On the way to school, Karen became fixated on the puzzle game on her phone! The game is played as fo ...

  10. C语言编写守护进程

    概念 守护进程(daemon)是一种运行在后台的一种特殊的进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件.由于在Linux中,每个系统与用户进行交流的界面成为终端,每一个从此终 ...