0.0 项目包管理
Window → Show View → Other→ Package Explorer
0.1 配置约束
Window → Preference → 搜索框cata → xml catalog → 解压mybatis.jar →org.apche.ibatis.builder.xml →mybatis-3-config.dta和mybatis-3-mapper.dtd
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集成时不需要这个环境 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc的事务 -->
<transactionManager type="JDBC"/>
<!-- 使用连接池 连接数据库 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm_mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
1.2配置各种Mapper.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="UserMapper">
<select id="selectUserById" parameterType="Integer" resultType="com.yanzi.bean.User">
select * from User where u_id = #{id}
</select>
<select id="selectUserByName" parameterType="String" resultType="com.yanzi.bean.User">
select * from User where u_username like '%${value}%'
</select>
</mapper>
1.3使用mybatis
package 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 org.junit.Test;
import com.yanzi.bean.User;
public class HelloMyBatis {
@Test
public void Test() throws IOException {
//读取配置文件
String resource="sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
//需要sqlSessionFactoryBuilder
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
//创建sqlSessionFactory
SqlSessionFactory ssf = ssfb.build(in);
//生成一个sqlsession
SqlSession session = ssf.openSession();
//操作数据库
List<User> list = session.selectList("UserMapper.selectUserByName", "王");
for (User u : list) {
System.out.println(u);
}
}
}
2.1动态代理
2.1.1 mapper.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.yanzi.mapper.UserMapper">
<select id="selectUserById" parameterType="Integer" resultType="com.yanzi.bean.User">
select * from User where u_id = #{id}
</select>
<select id="selectUserByName" parameterType="String" resultType="com.yanzi.bean.User">
select * from User where u_username like '%${value}%'
</select>
</mapper>
2.1.2 mapper的接口
package com.yanzi.mapper;
import com.yanzi.bean.User;
public interface UserMapper {
//通过Id查询一个用户
//1.接口方法名需要与mapper.xml的要调用的sql语句的id一致
//2.接口的形参类型需要与mapper.xml parameterType一致
//3.接口的返回值需要与mapper.xml resultType一致
//4.mapper.xml 中namespace要与接口的全包名一致
public User selectUserById(Integer id);
}
2.1.3 测试
package test;
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 org.junit.Test;
import com.yanzi.bean.User;
import com.yanzi.mapper.UserMapper;
public class MapperTest {
@Test
public void Test1() throws IOException {
String resource="sqlMapConfig.xml";
//读取配置文件
InputStream in = Resources.getResourceAsStream(resource);
//创建sqlSessionfactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
//生产一个sqlSession
SqlSession session = ssf.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.selectUserById(1);
System.out.println(user);
}
}
2.2 主配置文件
<?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="db.propertis" />
<!-- 推荐使用package包的形式来配置别名
包的形式会扫描主包及子包下的所有文件
以对象类名为别名,大小写不限,推荐使用小写
-->
<typeAliases>
<!-- <typeAlias type="com.yanzi.bean.User" alias="user" /> -->
<package name="com.yanzi.bean"/>
</typeAliases>
<!-- 使用spring集成时不需要这个环境 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc的事务 -->
<transactionManager type="JDBC"/>
<!-- 使用连接池 连接数据库 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- 配置映射器的位置:四种配置方法,推荐使用包的形式 -->
<mappers>
<!-- 第一种 -->
<!-- <mapper resource="mapper/UserMapper.xml"/> -->
<!-- <mapper url="file:\\\E:\Java\eclipse-workspace\ssm_mybatis\src\mapper\UserMapper.xml"/> -->
<!-- 第三种:UserMapper.xml和UserMapper.java接口要在同一个包下,并且名字相同 -->
<!-- <mapper class="com.yanzi.mapper.UserMapper"/> -->
<package name="com.yanzi.mapper"/>
</mappers>
</configuration>
2.3输入/输出映射 parameterType,resultType,resultMap(Map不需要对应数据库的列名)
<select id="selectUserByName" parameterType="String" resultType="com.yanzi.bean.User">
select * from User where u_username like '%${value}%'
</select>
<mapper>
<resultMap type="Country" id="county">
<result property="id" column="c_id"/>
</resultMap>
<select id="selectAll" resultMap="county">
select * from county
</select>
</mapper>
2.4 一对一关系
<mapper>
<resultMap type="Country" id="county">
<result property="id" column="c_id"/>
</resultMap>
<select id="selectAll" resultMap="county">
select * from county
</select>
<resultMap type="UserVo" id="uservolist">
<id property="u_id" column="u_username"/>
<result property="u_username" column="u_username"/>
<result property="u_sex" column="u_sex"/>
<association property="county" javaType="County" >
<id property="id" column="c_id"/>
<result property="c_countyname" column="c_countyname"/>
</association>
</resultMap>
<select id="selectAllUsreVo" resultMap="uservolist">
</select>
</mapper>
2.5 一对多关系
<mapper>
<resultMap type="CountryVo" id="countryVo">
<id property="id" column="c_id"/>
<result property="c_countryname" column="c_countyname"/>
<result property="c_capital" column="c_capital"/>
<!-- 一对多关系 -->
<collection property="userlist" ofType="User">
<id property="u_id" column="u_id" />
<result property="u_username" column="u_username"/>
</collection>
</resultMap>
</mapper>
2.6 动态sql:更方便的拼接sql语句
2.6.1 if(多条件查询)和where(解决if标签拼接字符串的AND符号问题) where 只能去掉前头的AND
<!-- 查询用户性别 模糊查询用户名 查询用户cid 国籍 -->
SELECT *
FROM user
where可以去掉前头的AND
<where>
<if test="u_sex!=null and u_sex!=''">
AND u_sex = #{u_sex}
</if>
<if test="u_username!=null and u_username!=''">
AND u_username LIKE "%"#{u_username}"%"
</if>
<if test="u_cid!=null and u_cid!=null">
AND u_cid=#{u_cid};
</if>
</where>
2.6.2 trim 可以去掉前头的AND,也可以去掉后头的AND
<!-- 查询用户性别 模糊查询用户名 查询用户cid 国籍 -->
SELECT *
FROM user
<!-- trim可以去掉前头的AND,也可以去掉后头的AND -->
<trim prefix="where" prefixOverrides="AND">
<if test="u_sex!=null and u_sex!=''">
u_sex = #{u_sex} AND
</if>
<if test="u_username!=null and u_username!=''">
u_username LIKE "%"#{u_username}"%" AND
</if>
<if test="u_cid!=null and u_cid!=null">
u_cid=#{u_cid} AND
</if>
</trim>
2.6.3 set标签解决更新数据表时字符串拼接逗号“,”问题
2.6.4 foreach
SELECT *
FROM user
WHERE u_id
IN
<!-- (1,2,3) -->
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
2.6.5 sql标签提取重复的sql语句