转自:http://ccchhhlll1988-163-com.iteye.com/blog/1415621
基本目的:利用Mybatis完成对一个表简单的select、insert、update、delete操作。达到熟悉MyBatis基本用法的目的。
用到的jar:MyBatis.jar,数据库驱动.jar(如mysql-connector.jar)
MyBatis的配置文件MyBatisConfig.xml。可以进行数据源和事务管理器的配置,以及其他影响MyBatis执行的变量。
MyBatisConfig.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="config.properties"></properties>
<!-- end properties -->
<!-- 定义vo -->
<typeAliases>
<typeAlias alias="User" type="com.mybatis.test.vo.User" />
</typeAliases>
<!-- end define -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mybatis/test/maps/UserMapper.xml" />
</mappers>
</configuration>
MyBatis的映射文件UserMapper.xml。文档中评价这个文件时奇迹发生的地方:
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.mybatis.test.dao.UserDAO">
<select id="selectUser" parameterType="int" resultType="User">
select *
from user where id = #{id}
</select>
<select id="selectAllUser" resultType="User">
select id, username from
user
</select>
<insert id="insertUser" parameterType="User">
insert into user(username)
values ( #{username})
</insert>
<update id="updateUser" parameterType="User">
update user set
username=#{username}, password=#{password} where id = #{id}
</update>
<delete id="deleteUser" parameterType="User">
delete from user where
username=#{username} and password=#{password}
</delete>
</mapper>
UserMapper接口文件,该文件与UserMapper.xml有着某些地方一一对应的关系,注意哦。
代码:
package com.mybatis.test.dao;
import java.util.List;
import com.mybatis.test.vo.User;
public interface UserDAO {
public User selectUser(int id);
public List<User> selectAllUser();
public int insertUser(User user);
public void updateUser(User user);
public void deleteUser(User user);
}
MyBatis文件,其中定义了产生SqlSessionFactory的工厂单例类。
代码:
package com.mybatis.test.util;
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;
/**
* @author chl 工厂单例模式,生成SqlSessionFactory实例。
*
*/
public class MyBatis {
private MyBatis() {
};
private static SqlSessionFactory ssf = null;
public static SqlSessionFactory getSessionFactory() throws IOException {
if (null == ssf) {
String resource = "mybatisConfig.xml";
Reader reader = Resources.getResourceAsReader(resource);
ssf = new SqlSessionFactoryBuilder().build(reader);
}// if
return ssf;
}
}
User文件,定义了User javabean实体类。
package com.mybatis.test.vo;
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 "id is:" + id + " name is: " + username;
}
}
userAction类,方法体中获取SqlSessionFactory实例,获得SqlSession,获取SqlMapper,然后调用映射的方法。
package com.mybatis.test.util;
import java.io.IOException;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.mybatis.test.dao.UserDAO;
import com.mybatis.test.vo.User;
/**
* @author chl 对user的操作。 其中将SqlSession放于方法级范围。
*/
public class UserAction {
public void insert(User user) {
SqlSessionFactory ssf = null;
SqlSession session = null;
try {
ssf = MyBatis.getSessionFactory();
session = ssf.openSession(true);// 设定事务自动提交(方式一)
UserDAO userDAO = session.getMapper(UserDAO.class);
userDAO.insertUser(user);
selectAll();
System.out.println("==================================");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != session) {
session.close();
session = null;
}
}
}
public void update(User user) {
SqlSessionFactory ssf = null;
SqlSession session = null;
try {
ssf = MyBatis.getSessionFactory();
session = ssf.openSession();
UserDAO userDAO = session.getMapper(UserDAO.class);
userDAO.updateUser(user);
session.commit();// 设定事务自动提交(二)
selectAll();
System.out.println("==================================");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != session) {
session.close();
session = null;
}
}
}
public void selectAll() {
SqlSessionFactory ssf = null;
SqlSession session = null;
try {
ssf = MyBatis.getSessionFactory();
session = ssf.openSession();
UserDAO userDAO = session.getMapper(UserDAO.class);
List<User> users = userDAO.selectAllUser();
for (User u : users) {
System.out.println(u.getUsername() + " " + u.getId());
}
System.out.println("session is null? " + (session == null));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != session) {
session.close();
session = null;
}
}
}
public void delete(User user) {
SqlSessionFactory ssf = null;
SqlSession session = null;
try {
ssf = MyBatis.getSessionFactory();
session = ssf.openSession(true);
UserDAO userDAO = session.getMapper(UserDAO.class);
userDAO.deleteUser(user);
selectAll();
System.out.println("==================================");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != session) {
session.close();
session = null;
}
}
}
public static void main(String[] args) {
UserAction userAction = new UserAction();
User user = new User();
user.setUsername("horizon");
user.setPassword("123456");
userAction.delete(user);
}
}
config.properties代码:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/tt
username=root
password=root
这是参照转载的网站代码修正的,转载的事务处理有点问题,在此进行了修改.....