做j2ee开发得熟悉好多的框架呀,来来玩下MyBatis!
第一步、到github.com下载jar包;
第二步、把demo项目结构弄好:
第三步、Mybatis-Configuration.xml这个是Mybatis的配置文件,这里面好像是有很多参数可以配置。我就主要配置两个用途的:
a、数据库连接;
b、如何找到具体的sql配置文件。
<?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/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- import UserMapper.xml -->
<mapper resource="com/cmpsky/mybatistest/data/UserMapper.xml"/>
</mappers>
</configuration>
<?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.cmpsky.mybatistest.dao.UserDao">
<select id="getUser" parameterType="com.cmpsky.mybatistest.data.UserModel" resultType="com.cmpsky.mybatistest.data.UserModel">
SELECT * FROM t_user_test WHERE username=#{username}
</select>
<insert id="addUser" parameterType="com.cmpsky.mybatistest.data.UserModel" flushCache="true">
INSERT INTO t_user_test (username,sex,age) VALUES (#{username},#{sex},#{age})
</insert>
<update id="updateUser" parameterType="com.cmpsky.mybatistest.data.UserModel">
UPDATE user SET age=#{age} WHERE username=#{username}
</update>
<delete id="deleteUser" parameterType="int">
DELETE FROM user WHERE username=#{username}
</delete>
</mapper>
第五步、弄个pojo对象:
第六步、弄个Dao类:
import java.io.IOException;
import java.io.InputStream;
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 com.cmpsky.mybatistest.data.UserModel;
public class UserDao
{
public static SqlSessionFactory sqlSessionFactory;
public static String resource = "com/cmpsky/mybatistest/data/Mybatis-Configuration.xml";
public static InputStream inputStream;
private SqlSession sqlSession;
static
{
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
public UserDao()
{
sqlSession = sqlSessionFactory.openSession();
}
public UserModel getUser(UserModel user)
{
return sqlSession.selectOne("com.cmpsky.mybatistest.dao.UserDao.getUser", user.getUsername());
}
public void insertUser(UserModel user)
{
sqlSession.insert("com.cmpsky.mybatistest.dao.UserDao.inserUser", user);
}
public void updateUser(UserModel user)
{
sqlSession.update("com.cmpsky.mybatistest.dao.UserDao.updateUser", user);
}
public void deleteUser(int id)
{
sqlSession.delete("com.cmpsky.mybatistest.dao.UserDao.deleteUser", id);
}
}
过程比起自己写sql再处理结果集,还是简化了的