使用流程
一: 环境搭建:导入两个包:1 mysql-connector-java-5.1.6-bin.jar(连接数据库)2 mybatis-3.3.1.jar(mabatis包)
二:创建数据库,创建表
三:写pojo对象,对应的mapper.java接口文件,对应的mapper.xml映射文件
1 接口文件UserMapper.java:
package com.db.mb.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.MapKey;
import com.db.mb.pojo.User;
public interface UserMapper {
public User getUser(int id);
public void insertUser(User user);
public int updateUser(User user);
public int deleteUser(int id);
public List<User> getUserList();
@MapKey("username")
public Map<Integer, User> getUserMap();
}
2 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.db.mb.dao.UserMapper">
<!-- 查询学生,根据id -->
<select id="getUser" parameterType="int" resultType="user">
select *
from user where id=#{id};
</select>
<!-- 增 -->
<insert id="insertUser" parameterType="user" useGeneratedKeys="true"
keyProperty="id">
insert into user
(password,username) values (
#{password}, #{username});
</insert>
<!-- 删 -->
<delete id="deleteUser" parameterType="int">
delete from user where
id=#{id};
</delete>
<!-- 改 -->
<update id="updateUser" parameterType="user">update uesr set password =
#{password} where id=#{id};
</update>
<!-- 获得查询集合 -->
<select id="getUserList" resultMap="visitorUser">
<include refid="getUserListSql" />
</select>
<resultMap id="visitorUser" type="user">
<id column="id" property="id" />
<result column="password" property="password" />
<result column="username" property="username" />
</resultMap>
<sql id="getUserListSql">select * from user</sql>
<!-- 查询获得map -->
<select id="getUserMap" resultMap="visitorUser" resultType="map">
<include refid="getUserMapSql" />
</select>
<sql id="getUserMapSql">select * from user</sql>
</mapper>
四:写sqlMapperConfig.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">
<!-- 此处是整个mybatis配置节点 mybatis靠此识别 -->
<configuration>
<!-- 别名定义 -->
<typeAliases>
<package name="com.db.mb.pojo"/>
</typeAliases>
<!-- 配置mybatis运行环境 -->
<environments default="development">
<environment id="development">
<!-- type="JDBC" 代表使用JDBC的提交和回滚来管理事务 -->
<transactionManager type="JDBC" />
<!-- mybatis提供了3种数据源类型,分别是:POOLED,UNPOOLED,JNDI -->
<!-- POOLED 表示支持JDBC数据源连接池 -->
<!-- UNPOOLED 表示不支持数据源连接池 -->
<!-- JNDI 表示支持外部数据源连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- sql映射 -->
<mapper resource="com/db/mb/dao/UserMapper.xml" />
</mappers>
</configuration>
五:写sqlSession对象获取工具类
package com.db.mb.mybatisutil;
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;
public class MybatisUtil {
private static SqlSessionFactory factory=null;
static{
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
InputStream inputStream=null;
try {
inputStream = Resources.getResourceAsStream("mybatis-config.xml");
} catch (IOException e) {
e.printStackTrace();
}
factory = builder.build(inputStream);
}
//获得session
public SqlSession getSession(){
return factory.openSession(true);
}
}
六 test类
package com.db.mb.test;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.ibatis.session.SqlSession;
import com.db.mb.dao.UserMapper;
import com.db.mb.mybatisutil.MybatisUtil;
import com.db.mb.pojo.User;
public class Test {
public static void main(String[] args) {
MybatisUtil util=new MybatisUtil();
SqlSession session = util.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
Map<Integer, User> userMap = mapper.getUserMap();
Set<Entry<Integer, User>> entrySet = userMap.entrySet();
for (Entry<Integer, User> entry : entrySet) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
session.close();
}
}
别名:针对pojo
1 <typeAliases>
<typeAlias alias="user" type="com,duobang.pojo.User"/>
<typeAliases/>
2
<typeAliases>
<package name="com.duobang.pojo"/>
<typeAliases/>
代码
@Alias(“user”)
public class Role{
}
返回map结果集
1 UserMapper.java接口中
@MapKey("username")
public Map<Integer, User> getUserMap();
2 UserMapper.xml配置中
<select id="getUserMap" resultMap="visitorUser" resultType="map">
<include refid="getUserMapSql" />
</select>