一个简单的POJO类
package com.test.pojo;
public class User {
private String name;
private String password;
public User(){
}
public User(String name,String password){
this.name=name;
this.password=password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
定义DAO接口UserMapper
package com.test.dao;
import com.test.pojo.User;
public interface UserMapper {
public void insertUser(User user);
public User getUser(String name);
}
不需要写DAO的实现,只需要创建一个UserMapper.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.test.dao.UserMapper"><!-- namespace是UserMapper接口的路径 -->
<insert id="insertUser" parameterType="User"><!-- id为接口中方法名,parameterType参数类型 -->
insert into user(name,password) values(#{name},#{password})
</insert>
<select id="getUser" resultType="User" parameterType="java.lang.String"><!-- resultType返回值类型 -->
select * from user where name=#{name}
</select>
</mapper>
MyBatis 还需要一个配置文件,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>
<settings>
<setting name="cacheEnabled" value="false" />
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
</settings>
<typeAliases>
<typeAlias alias="User" type="com.test.pojo.User"/>
</typeAliases>
<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/sample_db?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/test/dao/UserMapper.xml"/>
</mappers>
</configuration>
一个MyBatisUtil工具类
package com.test.data;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory;
static{
String str="config/mybatis-config.xml";
Reader reader=null;
try {
reader=Resources.getResourceAsReader(str);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
}
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
}
一个Test类 中测试
package com.test.dao.impl;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.test.dao.UserMapper;
import com.test.data.MyBatisUtil;
import com.test.pojo.User;
public class Test {
public static void main(String[] args) {
SqlSessionFactory ssf=MyBatisUtil.getSqlSessionFactory();
SqlSession sqlSession=ssf.openSession();
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
User user=new User("tom","sdfsdfd");
userMapper.insertUser(user);
sqlSession.commit();
}
}
数据库一张user表 name password两个字段,如有不明可下载附件