首页 > 代码库 > MyBatis的配置和用法

MyBatis的配置和用法

1、搭建MyBatis框架步骤:
1)导包:mybatis-3.2.5.jar(核心包);ojdbc14_11g.jar(连接数据库);
--------------------------------------------------------------------------------------------------------------------------------------------------
2)新建SqlMapConfig.xml配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<typeAliases>//使用别名可以在maper.xml中不必要写全路径
<typeAlias type="com.soft.entity.Dept" alias="Dept"/>
</typeAliases>
<!-- 配置连接 -->
<environments default="environment">
<environment id="environment">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="http://www.mamicode.com/oracle.jdbc.OracleDriver" /><!-- 注意驱动和url的准确性 -->
<property name="url" value="http://www.mamicode.com/jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="http://www.mamicode.com/test" />
<property name="password" value="http://www.mamicode.com/123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/soft/entity/Dept.xml"/><!-- Dept.xml所在的路径,注意不能使用. -->
</mappers>
</configuration>
--------------------------------------------------------------------------------------------------------------------------------------------------
PS:其他驱动:
mssql: "com.microsoft.jdbc.sqlserver.SQLServerDriver"
oracle jdbc: "oracle.jdbc.driver.OracleDriver"
sqlite: "org.sqlite.JDBC"
mySql: "com.mysql.jdbc.Driver"
PS:其他驱动URL:
sqlite3: "jdbc:sqlite:test.db"
oracle: "jdbc:oracle:thin:@server:1521:sid"
sqlserver: "jdbc:microsoft:sqlserver://server:1433;DatabaseName=barcode"
mySql: "jdbc:mysql://localhost:3306/test"
--------------------------------------------------------------------------------------------------------------------------------------------------
3)建立工具类获得连接:
a.获得配置文件名及其路径:private static final String CONFIG_PATH = "SqlMapConfig.xml";
b.将配置文件读取到IO流中: Reader reader = Resources.getResourcesAsReader(CONFIG_PATH);
c.利用SqlSessionFactoryBuilder解析配置文件,并新建sessionFaction工厂:
SqlSessionFactory sessionFaction = new SqlSessionFactoryBuilder().build(reader);
d.获取单例:(此为最佳的单例模式)
public static synchronized MyBatisUtil getInstance(){
if(null == instance){
synchronized (MyBatisUtil.class) {
instance = new MyBatisUtil();
}
}
return instance;
}
e.获取连接:
public synchronized SqlSession getSqlSession(){
session = sessionFaction.openSession();
return session;
}
--------------------------------------------------------------------------------------------------------------------------------------------------
4)配置实例xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"><!-- 3是MyBatis的版本 -->
<mapper namespace="01">
//如果数据库支持自动生成主键字段insert中可设置useGeneratedKeys="true",然后把keyProperty设置到目标字段上,如:keyProperty="user_id"
<insert id="addDept" parameterType="Dept" >
insert into G_Dept(deptno,dname,loc)values(#{deptno},#{dname},#{loc})
</insert>
<delete id="delDeptById" parameterType="java.lang.Integer">
delete from G_Dept where deptno= #{deptno}
</delete>
<update id="updateDept" parameterType="Dept">
update G_Dept set dname=#{dname},loc = #{loc} where deptno=#{deptno}
</update>
<select id="findAll" resultType="Dept">
select * from G_Dept
</select>
<select id="findAllMap" resultType="java.util.HashMap" parameterType="Dept">
select * from G_Dept
</select>
</mapper>

PS:parameterType——参数类型,即要传入的参数的类型;
resultType——返回类型
--------------------------------------------------------------------------------------------------------------------------------------------------
5)取得并使用:
Dept dept = new Dept();//一个Bean类
SqlSession session = MyBatisUtil.getInstance().getSqlSession();
session.delete("delDeptById", dept);
==================================================================================================================================================
2、oracl中自动增长列:
<insert id="addDept" parameterType="Dept" >
<selectKey keyProperty="d_no" order="BEFORE" resultType="java.lang.Integer">
select dept_seq.nextval from dual
</selectKey>
insert into G_Dept(d_no,d_name,u_no,d_level,d_addr)values(#{d_no},#{d_name},#{u_no},#{d_level},#{d_addr})
</insert>
==================================================================================================================================================
3、关联映射:
<!-- 多对一使用嵌套结果查询(推荐此使用此方法),
查询selectById2时,会关联到empById2进行查询,查询出来的结果匹配到Emp中的属性,Emp中有Dept属性,再次关联对应到Dept中的属性 -->
<select id="selectById2" parameterType="int" resultMap="empById2">
select a.*, b.* from g_emp a join g_dept b on (a.d_no = b.d_no) where a.e_no = #{id}
</select>
<resultMap type="Emp" id="empById2"><!-- type="Emp"查询的表所对应的bean类 -->
<id property="e_no" column="E_NO"/>
<result property="e_name" column="E_NAME"/>
<result property="d_no" column="D_NO"/>
<association property="dept" column="D_NO" resultMap="selectDept"></association><!-- 此处如果结果较多,建议再写一个resultMap,方便后续使用 -->
</resultMap>
<resultMap type="Dept" id="selectDept">
<id property="d_no" column="D_NO"/>
<result property="d_name" column="D_NAME"/>
<result property="u_no" column="U_NO"/>
<result property="d_level" column="D_LEVEL"/>
<result property="d_addr" column="D_ADDR"/>
</resultMap>
==================================================================================================================================================
4、集合映射:
<!-- 一对多使用嵌套结果查询(建议使用此方法) -->
<select id="selectByIdDept2" parameterType="int" resultMap="select4">
select a.*, b.* from g_dept a join g_emp b on(a.d_no = b.d_no) where a.d_no = #{id}
</select>
<resultMap type="Dept" id="select4">
<id property="d_no" column="D_NO"/>
<result property="d_name" column="D_NAME"/>
<result property="u_no" column="U_NO"/>
<result property="d_level" column="D_LEVEL"/>
<result property="d_addr" column="D_ADDR"/>
<collection property="emp" ofType="Emp" resultMap="selectEmp2"></collection><!-- ofType="Emp"关联的类型 -->
</resultMap>
<resultMap type="Emp" id="selectEmp2">
<id property="e_no" column="E_NO"/>
<result property="e_name" column="E_NAME"/>
<result property="d_no" column="D_NO"/>
</resultMap>
==================================================================================================================================================
5、返回Map类型:
<select resultType="java.util.Map"/> //Map中返回字段与value对应
List<Map> map = .......
map.get(i).get("大写字段名")
==================================================================================================================================================

MyBatis的配置和用法