(一)问候MyBatis3
第一节: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的更多相关文章
- SSM整合(三):Spring4与Mybatis3与SpringMVC整合
源码下载 SSMDemo 上一节整合了Mybatis3与Spring4,接下来整合SpringMVC! 说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目! 本节全部采用 ...
- SSM整合(二):Spring4与Mybatis3整合
上一节测试好了Mybatis3,接下来整合Spring4! 一.添加spring上下文配置 在src/main/resources/目录下的spring新建spring上下文配置文件applicati ...
- SSM集成(一):Mybatis3测试
Spring4+Mybatis3+SpringMVC(基于注解)整合步聚: 一)Mybatis3测试; 二)Mybatis3+Spring4整合; 三)Mybatis3+Spring4+SpringM ...
- 基于Spring4+SpringMVC4+Mybatis3+Hibernate4+Junit4框架构建高性能企业级的部标GPS监控平台
开发企业级的部标GPS监控平台,投入的开发力量很大,开发周期也很长,选择主流的开发语言以及成熟的开源技术框架来构建基础平台,是最恰当不过的事情,在设计之初就避免掉了技术选型的风险,避免以后在开发过程中 ...
- Mybatis3.x与Spring4.x整合(转)
http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype:cre ...
- MyBatis入门学习教程-Mybatis3.x与Spring4.x整合
一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype:create -DgroupId=me.gacl -DartifactId=spring4-myba ...
- MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合(转载)
孤傲苍狼 只为成功找方法,不为失败找借口! MyBatis学习总结(八)--Mybatis3.x与Spring4.x整合 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: m ...
- 《SSM框架搭建》二.mybatis3,spring4整合
感谢学习文章来自http://www.cnblogs.com/xdp-gacl/p/4271627.html,spring3修改为spring4.还有提示驱动过期的问题,是由于使用了mysql-con ...
- MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合
一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype:create -DgroupId=me.gacl -DartifactId=spring4-myba ...
随机推荐
- 【bzoj3926】【Zjoi2015】诸神眷顾的幻想乡
题解: 如果树上某个路径串的端点不是叶子,那么一定是另一个串的子串: 这样只对叶子$dfs$把$20*20$个串插入广义$SAM$就是统计本质不同的串的个数的模板了: 我不太会分析广义$SAM$的空间 ...
- 使用 laravel 的 queue 必须知道的一些事
1. 在修改 queue 相关代码后,必须要使用 php artisan queue:restart 来重启队列服务,否则所做的修改可能不会生效(没法重现了,按理说应该和使用 queue:listen ...
- 【转】基于R语言构建的电影评分预测模型
一,前提准备 1.R语言包:ggplot2包(绘图),recommenderlab包,reshape包(数据处理) 2.获取数据:大家可以在明尼苏达州大学的社会化计算研 ...
- MySQL搭建环境
一.MySQL安装 Windows下安装参考网址:https://blog.csdn.net/NepalTrip/article/details/79492058 Ubuntu Linux下安装参考网 ...
- python scrapy 基本操作演示代码
# -*- coding: utf-8 -*- import scrapy # from quotetutorial.items import QuoteItem from quotetutorial ...
- HDU 4548 美素数 在线打表加数状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4548 解题报告:一开始本想先打个素数表,然后每次输入L 跟R 的时候都进行暴力判断,但这题测试数据太多 ...
- 【leetcode 简单】 第九十一题 找不同
给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. 示例: 输入: s = "abcd&quo ...
- Python练习-递归二分算法
# 编辑者:闫龙 #递归,二分算法演示 l = [i for i in range(1,100)]#定义一个列表l,并追加1-99的所有数字 def FindNum(num,l):#定义函数FindN ...
- 20145202马超 2016-2017-2 《Java程序设计》第三次实验
实验三 敏捷开发与XP实践 http://www.cnblogs.com/rocedu/p/4795776.html, Eclipse的内容替换成IDEA 在IDEA中使用工具(Code->Re ...
- sqlplus设置长度
1.set linesize 100 2.col XX format a30 3.col XXX format 9,999,999,999 3.set heading off 表头不显示