- 原始dao开发
步骤:
1.1 全局配置文件编写 – SqlMapConfig.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>
<!-- spring整合后enviroments配置将废除 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理,事务由mybatis控制 -->
<transactionManager type="JDBC" />
<!-- 数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/user" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="xml/User.xml"/>
</mappers>
</configuration>
1.2 配置映射文件编写 -- 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">
<!-- namespace命名空间 -->
<mapper namespace="user">
<!-- 根据用户id查询用户信息
id:唯一标识符,标识映射文件中的sql
parameterType:指定输入参数的类型
resultType:指定sql输出的java对象
#{}表示一个占位符,相当于sql中的?占位符
#{id}表示接收输入的参数
-->
<select id="findUserById" parameterType="int" resultType="cn.wwmtek.mybatis.po.User">
select * from user where id = #{id}
</select>
<!-- 获取用户表中某些字段 -->
<select id="findUserColumnById" parameterType="int" resultType="cn.wwmtek.mybatis.po.User">
select id, username from user where id = #{id}
</select>
<!-- 根据用户名查询用户 -->
<select id="findUserByUserName" parameterType="java.lang.String" resultType="cn.wwmtek.mybatis.po.User">
select * from user where username like '%${value}%'
</select>
<!-- 插入用户 -->
<insert id="insertUser" parameterType="cn.wwmtek.mybatis.po.User">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into user(username, birthday, sex, address) values(#{username}, #{birthday}, #{sex}, #{address})
</insert>
<!-- 更新用户 -->
<update id="updateUser" parameterType="cn.wwmtek.mybatis.po.User">
update user set username = #{username}, sex = #{sex} where id = #{id}
</update>
<!-- 删除用户 -->
<update id="deleteUser" parameterType="java.lang.Integer">
delete from user where id = #{id}
</update>
</mapper>
1.3 编写dao接口
public interface UserDao {
public User findUserById(int id) throws Exception;
public List<User> findUserByUserName(String name) throws Exception;
public void deleteUserById(int id) throws Exception;
}
1.4 编写dao实现类
public class UserDaoImpl implements UserDao {
private SqlSessionFactory sqlSessionFactory;
public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
@Override
public User findUserById(int id) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
User user = sqlSession.selectOne("user.findUserById", 1);
return user;
}
@Override
public List<User> findUserByUserName(String name) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
List<User> userList = sqlSession.selectList("user.findUserByUserName", "小小");
return userList;
}
@Override
public void deleteUserById(int id) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.delete("user.deleteUser", 34);
sqlSession.commit();
sqlSession.close();
}
}
1.5 使用Junit测试
public class UserDaoImplTest {
SqlSessionFactory sqlSessionFactory = null;
@Before
public void setUp() throws IOException {
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testFindUserById() throws Exception {
UserDao userDao = new UserDaoImpl(sqlSessionFactory);
User user = userDao.findUserById(1);
System.out.println(user.toString());
}
}
2.mapper代理方法
步骤
2.1 全局配置文件编写 – SqlMapConfig.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>
<!-- spring整合后enviroments配置将废除 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理,事务由mybatis控制 -->
<transactionManager type="JDBC" />
<!-- 数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/user" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/wwmtek/mybatis/xml/UserMapper.xml"/>
</mappers>
</configuration>
2.2 编写mapper映射文件
<?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">
<!-- namespace命名空间 -->
<mapper namespace="user">
<!-- 根据用户id查询用户信息
id:唯一标识符,标识映射文件中的sql
parameterType:指定输入参数的类型
resultType:指定sql输出的java对象
#{}表示一个占位符,相当于sql中的?占位符
#{id}表示接收输入的参数
-->
<select id="findUserById" parameterType="int" resultType="cn.wwmtek.mybatis.po.User">
select * from user where id = #{id}
</select>
<!-- 获取用户表中某些字段 -->
<select id="findUserColumnById" parameterType="int" resultType="cn.wwmtek.mybatis.po.User">
select id, username from user where id = #{id}
</select>
<!-- 根据用户名查询用户 -->
<select id="findUserByUserName" parameterType="java.lang.String" resultType="cn.wwmtek.mybatis.po.User">
select * from user where username like '%${value}%'
</select>
<!-- 插入用户 -->
<insert id="insertUser" parameterType="cn.wwmtek.mybatis.po.User">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into user(username, birthday, sex, address) values(#{username}, #{birthday}, #{sex}, #{address})
</insert>
<!-- 更新用户 -->
<update id="updateUser" parameterType="cn.wwmtek.mybatis.po.User">
update user set username = #{username}, sex = #{sex} where id = #{id}
</update>
<!-- 删除用户 -->
<update id="deleteUser" parameterType="java.lang.Integer">
delete from user where id = #{id}
</update>
</mapper>
2.3 编写mapper对应接口
public interface UserMapper {
public User findUserById(int id) throws Exception;
public List<User> findUserByUserName(String username) throws Exception;
}
2.4 使用Junit测试
public class UserMapperTest {
private SqlSessionFactory sqlSessionFactory;
@Before
public void setUp() throws IOException {
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testFindUserById() throws Exception {
SqlSession sqlSession = null;
try {
sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.findUserById(1);
System.out.println(user);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(sqlSession != null) {
sqlSession.close();
}
}
}
}