一、先建一个数据库test,再建一张user表,表里有两个字段:id 是自增长,name。
二、新建一个java项目,导入两个jar包:mysql-connector-java-5.1.17-bin.jar 和 mybatis-3.0.4.jar。
三、在src下建一个包:com.cb
四、在com.cb包下面有四个文件,可以直接复制粘贴进去:
1、 User.java
package com.cb;
public class User {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(){
}
}
2、configuration.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>
<typeAliases>
<typeAlias type="com.cb.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/test"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/cb/User.xml"/>
</mappers>
</configuration>
3、User.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.cb.User">
<!-- 增加一条记录 -->
<insert id="addOneUser" parameterType="User" useGeneratedKeys="true" keyProperty="id">
insert into user(name)
values(#{name})
</insert>
<!-- 删除一条记录 -->
<delete id="deleteOneUser" parameterType="User">
delete from user where id=#{id}
</delete>
<!-- 更新一条记录 -->
<update id="updateOneUser" parameterType="User">
update user set name=#{name} where id=#{id}
</update>
<!-- 查询一条记录 -->
<select id="selectOneUser" resultType="User" parameterType="int">
select * from user where id = #{id}
</select>
<!-- 查询全部记录 -->
<select id="selectAllUser" resultType="User" parameterType="int">
select * from user
</select>
</mapper>
4、 Test.java
package com.cb;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
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 Test {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException{
String resource = "com/cb/configuration.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
try {
//增加一条新的记录
User user=new User();
user.setName("wangwu");
session.insert("com.cb.User.addOneUser",user);
session.commit();
//删除一条记录
/*session.delete("com.cb.User.deleteOneUser",11);
session.commit();*/
//更新一条数据
/*User user=new User();
user.setName("lisi");
user.setId(2);
session.update("com.cb.User.updateOneUser",user);
session.commit();*/
//按条件查询一条数据
/*User user = (User) session.selectOne("com.cb.User.selectOneUser",8);
System.out.println("id\tname");
System.out.println(user.getId()+"\t"+user.getName());*/
//查询表的全部数据
ArrayList<User> list = (ArrayList<User>) session.selectList("com.cb.User.selectAllUser");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).getId()+"\t"+list.get(i).getName());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
session.close();
}
}
}