项目源码下载:MyBatis 提供MyEclipse和Eclipse下的项目源码以及war文件
一,项目搭建
打开MyEclipse或Eclipse创建一个Web项目
在lib目录下导入相关的jar包
二,创建数据库
在本地创建一个数据库mybatis
新建一张表t_user 表结构如下:
- id int型 主键 用户编号
- username varchar型 用户名
- password varchar型 用户密码
三,创建实体类
在src目录下创建包cn.itcast.user 并在该包下新建类User.java
package cn.itcast.user;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
}
四,配置映射文件
在cn.itcast.user包下创建文件 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="cn.itcast.user.userMapper">
<select id="findAll" resultType="cn.itcast.user.User">
select * from t_user
</select>
<select id="findById" resultType="cn.itcast.user.User">
select * from t_user where id =#{id}
</select>
<insert id="insertOneUser">
insert into t_user (id,username,password)
values (#{id},#{username},#{password})
</insert>
<update id="updateUser">
update t_user set
username=#{username},
password=#{password}
where id=#{id}
</update>
<delete id="deleteUser">
delete from t_user where id=#{id}
</delete>
</mapper>
五,配置核心配置文件
在src目录下新建配置文件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>
<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/mybatis" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/itcast/user/userMapper.xml" />
</mappers>
</configuration>
数据库用户名和密码因根据你的本地数据库的用户名和密码改写配置文件
六,创建测试类
在src目录下创建包cn.itcast.test 并在该包下创建类Test.java
package cn.itcast.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import cn.itcast.user.User;
public class Test {
public SqlSessionFactory getSqlSessionFactory() throws IOException{
String path = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(path);
return new SqlSessionFactoryBuilder().build(inputStream);
}
@org.junit.Test
public void findAll() throws IOException{
SqlSessionFactory sessionFactory = getSqlSessionFactory();
SqlSession openSession = sessionFactory.openSession();
List<User>users = openSession.selectList("cn.itcast.user.userMapper.findAll");
for (User user : users) {
System.out.println(user);
}
openSession.close();
}
@org.junit.Test
public void findById() throws IOException{
SqlSessionFactory sessionFactory = getSqlSessionFactory();
SqlSession openSession = sessionFactory.openSession();
User user = openSession.selectOne("cn.itcast.user.userMapper.findById",1);
System.out.println(user);
openSession.close();
}
@org.junit.Test
public void Insert() throws IOException{
SqlSessionFactory sessionFactory = getSqlSessionFactory();
SqlSession openSession = sessionFactory.openSession();
User user = new User();
user.setId(3);
user.setUsername("tom");
user.setPassword("123456");
openSession.insert("cn.itcast.user.userMapper.insertOneUser", user);
openSession.commit();
openSession.close();
findAll();
}
@org.junit.Test
public void update() throws IOException{
SqlSessionFactory sessionFactory = getSqlSessionFactory();
SqlSession openSession = sessionFactory.openSession();
User user = openSession.selectOne("cn.itcast.user.userMapper.findById",3);
user.setUsername("nike");
user.setPassword("33333");
openSession.update("cn.itcast.user.userMapper.updateUser", user);
openSession.commit();
openSession.close();
findAll();
}
@org.junit.Test
public void delete() throws IOException{
SqlSessionFactory sessionFactory = getSqlSessionFactory();
SqlSession openSession = sessionFactory.openSession();
openSession.delete("cn.itcast.user.userMapper.deleteUser", 3);
openSession.commit();
openSession.close();
findAll();
}
}
首先在t_user中手动插入两条记录:
Junit可在项目右键-properties-build path -library-add library -Junit
从上到下分别运行所有方法:
选中方法-右键-runas-junit
findAll:
findById:
Insert:
update:
delete:
可以看到都正确的在控制台打印了结果