目录
jar包
配置文件
外置资源文件database.properties
driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/smbms?useUnicode\=true&characterEncoding\=utf8
user=root
pwd=123
总配置文件mybatis-config.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>
<!--设置外置配置文件-->
<properties resource="database.properties"></properties>
<!--设置各种参数,包括类别名-->
<settings>
<setting name="logImpl" value="LOG4J" />
</settings>
<!--设置类别名 p29-->
<typeAliases>
<package name="cn.smbms.entity"/>
</typeAliases>
<!--p30 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${user}" />
<property name="password" value="${pwd}" />
</dataSource>
</environment>
</environments>
<!--p31 -->
<mappers>
<!-- 注册userMapper.xml文件, userMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/userMapper.xml -->
<mapper resource="cn/smbms/dao/user/UserMapper.xml"/>
<mapper resource="cn/smbms/dao/role/RoleMapper.xml"/>
<mapper resource="KET/KETMapper.xml"/>
</mappers>
</configuration>
日志文件log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
xml配置文件头部GoodsMapper.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">
xml完整文件GoodsMapper.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.bdqn.dao.ElectronicMapper">
<select id="findAll" resultType="Electronic">
select * from electronic
</select>
</mapper>
对应的dao接口GoodsMapper.java
package com.bdqn.dao;
import java.util.List;
import com.bdqn.entity.Goods;
public interface GoodsMapper {
public List<Goods> findAllGoods();
}
测试类:Test.java
工具类:可以把获取链接的过程封装成一个工具类
package com.bdqn.utils;
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 MyBatis {
//定义一个factory
private static SqlSessionFactory factory;
//静态代码块,项目开始先执行一次,读取配置文件,给sqlsessionfactoty赋值
static{
InputStream is;
try {
is = Resources.getResourceAsStream("mybatis-config.xml");
factory = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//静态方法,只需类名点方法名就能获取sqlsession
public static SqlSession createSqlSession(){
return factory.openSession(false);//true为自动提交事务
}
//关闭sqlsession
public static void closeSqlSession(SqlSession session){
if (null!=session) {
session.close();
}
}
}
test类
package text;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.bdqn.dao.GoodsMapper;
import com.bdqn.entity.Goods;
import com.bdqn.utils.MyBatis;
public class Test {
public static void main(String[] args) {
SqlSession session = MyBatis.createSqlSession();
List<Goods> list =
session.getMapper(GoodsMapper.class).findAllGoods();
for (Goods goods : list) {
System.out.println(goods.getId());
System.out.println(goods.getName());
System.out.println(goods.getPrice());
}
}
}
以上就是基本mybatis开发的过程
接下来还有些要补充的的东西
根据传入的参数不同可以分成四种
//无参
List<Goods> FindAllxx();
//单个参数
List<Goods> FindAll1(String name);
//使用注解传入对应的参数--多个参数
List<Goods> FindAll2(@Param("name3") String name,@Param("price3") double price);
//对象参数
List<Goods> FindAll3(Goods goods);
//集合参数
List<Goods> FindAll4(Map<String, Object> map);
参数返回类型resultMap
<!-- 使用resultmap映射查询结果集 -->
<resultMap type="provider" id="proMAP">
<result property="proName" column="proName"/>
<result property="billName" column="productName"/>
</resultMap>
<!-- 用resultMap结果映射来控制输出的内容 -->
<select id="findAllSSS" resultMap="proMAP">
SELECT * FROM smbms_provider p,smbms_bill b WHERE p.id = b.providerId
</select>
resultMap包含一对一association的链接查询
<!-- 上机练习3 p59 -->
<resultMap type="Bill" id="selectMap2">
<id property="id" column="id"/>
<result property="billCode" column="billCode"/>
<result property="productName" column="productName"/>
<result property="totalPrice" column="totalPrice"/>
<result property="isPayment" column="isPayment"/>
<!-- 这里的property是实体类的属性名,后面那个是对应的类名 -->
<association property="provider" javaType="Provider">
<result property="proCode" column="proCode"/>
<result property="proName" column="proName"/>
<result property="proContact" column="proContact"/>
<result property="proPhone" column="proPhone"/>
</association>
</resultMap>
<select id="selectBill_2" resultMap="selectMap2">
SELECT * FROM smbms_bill b,smbms_provider p WHERE b.providerId =
p.id AND b.productName LIKE "%"#{productName}"%" AND b.isPayment = #{isPayment}
AND p.id=#{providerId}
</select>
<association property="classes" javaType="one.to.many.Classes">
<id column="cid" property="cid"/>
<result column="cname" property="cname"/>
</association>
resultMap包含多对一collection的链接查询查看一对一和一对多
<!-- 上机练习4 -->
<resultMap type="Provider" id="selectPro">
<id property="id" column="id"/>
<result property="proCode" column="proCode"/>
<result property="proName" column="proName"/>
<result property="proContact" column="proContact"/>
<result property="proPhone" column="proPhone"/>
<collection property="bills" ofType="Bill">
<!-- 注意这里的区分可以用任何符号来区分,只要不是id -->
<id property="id" column="-id"/>
<result property="billCode" column="billCode"/>
<result property="productName" column="productName"/>
<result property="totalPrice" column="totalPrice"/>
<result property="isPayment" column="isPayment"/>
</collection>
</resultMap >
<select id="selectP" resultMap="selectPro">
SELECT * FROM smbms_provider p,smbms_bill b WHERE p.id= #{id} AND p.id=b.providerId
</select>
<collection property="students" ofType="one.to.many.Student">
<id column="sid" property="sid"/>
<result column="sname" property="sname"/>
</collection>
动态SQL
<!-- 动态SQL p77 上机练习3 -->
<update id="updateProvider3333" parameterType="provider">
UPDATE smbms_provider
<set>
<if test="proCode!=null">proCode=#{proCode},</if>
<if test="proName!=null">proName=#{proName},</if>
<if test="proDesc!=null">proDesc=#{proDesc},</if>
<if test="proContact!=null">proContact=#{proContact},</if>
<if test="proPhone!=null">proPhone=#{proPhone},</if>
<if test="proAddress!=null">proAddress=#{proAddress},</if>
<if test="proFax!=null">proFax=#{proFax},</if>
<if test="createdBy!=null">createdBy=#{createdBy},</if>
<if test="creationDate!=null">creationDate=#{creationDate},</if>
<if test="modifyBy!=null">modifyBy=#{modifyBy},</if>
<if test="modifyDate!=null">modifyDate={modifyDate},</if>
</set>
WHERE id=#{id}
</update>
<!-- 动态SQL p72 -->
<select id="findAll3333" resultType="provider" parameterType="provider">
select * from smbms_provider
<where>
<if test="proName!=null and proName!=''">
and proName LIKE "%"#{proName}"%"
</if>
<if test="proCode!=null and proCode!=''">
and proCode = #{proCode}
</if>
</where>
</select>
<!-- p88页上机练习7 使用choose语句查询供应商列表 -->
<select id="selectPR88" resultType="Provider">
select * from smbms_provider where 1=1
<choose>
<when test="proCode!=null and proCode!=''">
and proCode like "%"#{proCode}"%"
</when>
<when test="proName!=null and proName!=''">
and proName like "%"#{proName}"%"
</when>
<when test="proContact!=null and proContact!=''">
and proContact like "%"#{proContact}"%"
</when>
<otherwise>
and creationDate > "2018-01-01 00:00:00"
</otherwise>
</choose>
</select>
<update id="updateProvider4444" parameterType="provider">
UPDATE smbms_provider
<trim prefix="set" suffixOverrides="," suffix="WHERE id=#{id}">
<if test="proCode!=null">proCode=#{proCode},</if>
<if test="proName!=null">proName=#{proName},</if>
<if test="proDesc!=null">proDesc=#{proDesc},</if>
<if test="proContact!=null">proContact=#{proContact},</if>
<if test="proPhone!=null">proPhone=#{proPhone},</if>
<if test="proAddress!=null">proAddress=#{proAddress},</if>
<if test="proFax!=null">proFax=#{proFax},</if>
<if test="createdBy!=null">createdBy=#{createdBy},</if>
<if test="creationDate!=null">creationDate=#{creationDate},</if>
<if test="modifyBy!=null">modifyBy=#{modifyBy},</if>
<if test="modifyDate!=null">modifyDate={modifyDate},</if>
</trim>
</update>
<select id="selectP333" resultMap="selectPro1">
<foreach collection="list" open="(" separator="," close=")" item="list">
#{list}
</foreach>
</select>