一、MyBatis入门
1、简单示例
# 创建一个名称为mybatis的数据库
create database mybatis;
# 使用名称为mybatis的数据库
use mybatis;
# 创建一个tb_user表,
CREATE TABLE tb_user (
id int(11) primary key auto_increment,
name varchar(18) default null,
sex char(2) default null,
age int(2) default null
)
package org.wqc.model;
public class User {
private Integer id;
private String name;
private String sex;
private Integer 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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
//无参构造函数 pojo可以没有这个
public User(){
}
//有参构造函数 pojo可以没有这个
public User(String name, String sex, Integer age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
}
<?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="org.wqc.mapper.UserMapper">
<insert id="save" parameterType="org.wqc.model.User" useGeneratedKeys="true">
INSERT INTO tb_user(name,sex,age) values(#{name},#{sex},#{age})
</insert>
</mapper>
<?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">
<!-- XML配置文件包含对MyBatist系统的核心设置 -->
<configuration>
<!-- 指定Mybatis所用日志的具体实现 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<!-- 环境配置,即连接的数据库 -->
<environments default="mysql">
<environment id="mysql">
<!-- 指定事务管理类型,直接简单使用了JDBC的提交和回滚设置 -->
<transactionManager type="JDBC"/>
<!-- dataSource指数据源配置,POOLED是JDBC连接对象的数据源连接池的实现 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- mappers告诉MyBatis 去哪里找持久化类的映射文件 -->
<mappers>
<mapper resource="org/wqc/mapper/UserMapper.xml"/>
</mappers>
</configuration>
public class MyBatisTest {
public static void main(String[] args) throws IOException {
// 读取mybatis-config.xml文件
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
// 初始化mybatis,创建SqlSessionFactory类的实例
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 创建Session实例
SqlSession session = sqlSessionFactory.openSession();
// 创建User对象
User user = new User("admin","男",27);
// 插入数据
session.insert("org.wqc.mapper.UserMapper.save", user);
// 提交事务
session.commit();
// 关闭Session
session.close();
}
}
log4j.rootLogger=ERROR, stdout
log4j.logger.org.wqc.mapper.UserMapper=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
本页教程Eclipse项目 package explorer如下图, 源码下载:http://download.csdn.net/detail/wqc19920906/9883506