1.mybatis.xml
2.mapper.xml
==============================================================
1.mybatis.xml
<configuration>是mybatis.xml文件的根元素
1)数据库相关标签
I <environments>表示这个标签中存放的至少一个数据库的连接信息,一个environment标签存放着一个数据库连接信息
每个environment通过id值进行区分,id可以任意取名
<environments default="environment的id值" > default表示当前默认使用的数据库环境
II <transactionManager type="JDBC" >指定事务类型
2种事务类型:JDBC 指定jdbc中的Connection按照commit、rollback进行提交、回滚
MANAGED 由容器处理事务
III dataSource:表示数据源
type:数据源类型
1)POOLED:使用数据库连接池 PooledDataSource ,线程安全的连接池
2)UNPOOLED: 不使用连接池 UnPooledDataSource(了解)
3) JNDI : 命名和目录服务(了解)
IV property中的数据库连接四大要素driver url username password,这四个作为name不可变,将username改成 uname报错,但是四个property位置可以颠倒
V 将数据库连接条件写入properties文件中,在当前mybatis.xml文件中使用
<properties resource="properties文件路径" />导入mybatis.xml中,在<dataSource>下的property的value使用 ${jdbc.driver}
补充:jdbc.properties
数据的存放采用key=value的形式,每行可以不添加“;”,采用 前缀.driver 这种形式可根据前缀不同,连接不同数据库
mybatis.xml文件中使用<properties resource="properties文件路径" />,property 的value=${key}
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名
jdbc.uname=root
jdbc.pwd=123
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/数据库名
mysql.uname=root
mysql.pwd=123
2)mapper注册标签
<mappers>注册mapper.xml文件的根标签,字标签mapper
第一种方式:<mapper resource="mapper.xml的路径" /> resource中使用路径格式,在classpath下找文件
第二种方式:<package name="xml所在的包" /> 例如:<package name="com.ljf.bao" />
package这种形式可以自动将这个包下的xml文件进行注册
package使用条件:
1. mapper文件名必须和Dao接口名一样 2. mapper文件必须和Dao接口在同一目录
3)其他常用配置
<settings>在字标签setting中进行一些基本配置
例如:<setting name="logImpl" value="STDOUT_LOGGING" /> 将执行的sql语句打印到控制台
<plugins>在这个标签进行插件注册。plugin本身就是插件的意思
例如:<plugin interceptor="com.github.pagehelper.PageInterceptor" /> 注册分页插件
<typeAliases> 定义resultType使用的别名
第一种方式: <typeAlias type=" 全限定类型名称 " alais=" 自定义别名 " />用来定义一个类型的别名
例如:<typeAlias type="com.ljf.domain.Student" alias="mystudent" />
第二种方式:<package name="实体类文件所在包名" />
name:包名,表示包中的类名就是别名, 这个包中的所有类的 类名是别名(不用区分大小写)
例如:<package name="com.ljf.domain" />
mybatis.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="jdbc.properties" />
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<!--<typeAlias type="com.ljf.domain.Student" alias="mystudent" />-->
<package name="com.ljf.domain" />
</typeAliases>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor" />
</plugins>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<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>
<environment id="test">
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--
<mapper resource="com/ljf/dao/StudentDao.xml"/>
-->
<package name="com.dao" />
</mappers>
</configuration>
2.mapper.xml
标签sql 存放常用的sql语句段,同过id来进行使用。使用时:<include refid=" sql的id " />
标签resultMap 设置返回值,是将表字段与实体类属性进行匹配。使用时使用resultMap=“resultMap的id值”,不用resultType
属性id 表示可以通过获取 id 来使用返回值类型
属性type java对象的类型, 一般使用类的全限定名称。sql语句执行结果的ResultSet会转为这个type指定的类型
子标签id 主键列使用id标签
子标签result 非主键列使用result标签
两个属性: column 数据库表的字段 property 实体类的属性名
标签select insert update delete 属性id:接口对应的方法名 resultType:执行结果的返回值类型
子标签resultType:方法中返回值是List时,在xml文件中没有返回 List 这种方式,将返回List的返回值类型设置为
List<这个位置的类型的全限定名>
当在mybatis.xml文件中typeAliases使用package声明,则可以直接使用返回值的实体类类名;或者使用 typeAlias 重新声明,则可以使用声明的别名
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ljf.dao.StudentDao">
<sql id="studentField">
select id,name, email, age from student
</sql>
<resultMap id="customMap" type="com.ljf.vo.Customer" >
<id column="id" property="cid" />
<result column="name" property="cname" />
<result column="email" property="cemail" />
<result column="age" property="cage"/>
</resultMap>
<select id="selectResultTypeStudentById" resultMap="customMap">
select id ,name ,email ,age from student where id=#{id}
</select>
<select id="selectStudentWhere" resultType="com.ljf.domain.Student">
select id,name, email, age from student
<where>
<if test="name !=null and name !=''">
name = #{name}
</if>
<if test="age != null ">
and age < #{age}
</if>
</where>
</select>
<select id="selectStudentForeachList2" resultType="com.ljf.domain.Student">
<!-- 引用sql片段 -->
<include refid="studentField" />
<if test="list !=null and list.size > 0 ">
where id in (
<foreach collection="list" open="" close="" item="stu" separator=",">
#{stu.id}
</foreach>
)
</if>
</select>
</mapper>
3)拓展
拓展一:mapper.xml文件数据库操作语句原理(以select为例)
<select id="selectResultTypeStudentById" resultType="com.ljf.entity.Student">
select id ,name ,email ,age from student where id=#{id}
</select>
select执行过程:
返回值是对象
resultType:使用别名或全限定名称
ResultSet rs = pst.execuetQuery(" select id,name,email,age from student where id=#{id}")
while(rs.next){
Student student = new Student();
student.setId(rs.getInt("Id"));
student.setName(rs.getString("name"))
...
}
拓展二:省略mapper.xml文件
StudentDao.java
@Mapper
public interface StudentDao {
@Insert("insert into student values(#{id},#{name},#{email},#{age})")
int insertStudent(Student student);
@Update("update student set name=#{name},email=#{email},age=#{age} where id=#{id} ")
int updateStudent(Student student);
@Delete("delete from student where id =#{stuid}")
int deleteStudentById(Integer id);
@Select("select id,name,email,age from student where id=#{stuid}")
Student selectStudentById(Integer id);
}
@Mapper: 当前接口就相当于 mapper配置文件
在mybatis.xml文件中只能使用package进行注册
<mappers>
<package resource="StudentDao.java所在的包" />
</mappers>
在方法上面可以使用@Insert @Update @Delete @Select 进行注册,小括号中写方法对应的数据库查询语句
拓展三:关于上面两个xml文件的标签
在两个xml文件开头都有一个限制文件mybatis-3-mapper.dtd和mybatis-3-config.dtd,里面声明了当前文件可以使用的标签和各个标签的属性
mybatis-3-mapper.dtd和mybatis-3-config.dtd解读:https://blog.csdn.net/ljf12138/article/details/102865440