架包和日志的配置文件不一一列举了
一.核心配置文件
<?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="datainfo.properties"></properties>
<!--
typeAliases用来给类起别名的,简化全类名的编写
typeAliases中可以有多个typeAlias元素,每一个就是针对一个类定义的别名
type:要设定别名的类的全类名 alias:别名
mybatis内置的别名
Integer Long Double List Map Set
int long
package元素中name属性扫描实体属性所在的包,别名默认为实体类的的名
-->
<typeAliases>
<!-- <typeAlias type="mybaentity.User" alias="User"/> -->
<!-- <package name=""/>是扫描哪个包下面的所有接口 -->
<package name="mybaentity"/>
</typeAliases>
<!-- default与id的名称相同,根据id的名称找到数据库 -->
<environments default="mybati">
<environment id="mybati">
<!--
处理事物的类型
事务管理:
jdbc:以传统的jdbc方式来提交回滚事物,设置了事物,自动提交就会关闭
MANAGED:以第三方框架的形式来管理事物(spring,Tomcat)
-->
<transactionManager type="jdbc"></transactionManager>
<!--
数据库连接池类型
数据库连接池:
POOLED:使用连接池
UNPOOLED:不使用连接池
JNDI:
-->
<dataSource type="POOLED">
<!-- 链接数据库的流程 -->
<property name="driver" value="${DRIVER}" />
<property name="url" value="${DBURL}" />
<property name="username" value="${USERNAME}" />
<property name="password" value="${USERPWD}" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 映射找到对应的mapper的核心管理文件,注意只能用斜杠 -->
<!-- <mapper resource="mybaentity/mapper/User.xml"></mapper> -->
<!-- 映射到整个包来加载 用扫描包xml必须与对应接口同名同包-->
<package name="mybaentity.mapper"/>
</mappers>
</configuration>
二 sql配置文件
<?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="mybaentity.mapper.UserMapper">
<select id="getSelectId" parameterType="int" resultType="User">
select * from user where userid = #{userid}
</select>
<select id="selectAll" resultType="User">
select * from user
</select>
</mapper>
三.链接数据库的文件
DRIVER=com.mysql.jdbc.Driver
DBURL=jdbc:mysql://localhost:3306/demo06?useUnicode=true&characterEncoding=utf8
DBNAME=ORCL
USERNAME=root
USERPWD=940906
四.如何利用Mybatis进行数据交互
//利用mybatis的API根数据库进行交互
//加载核心配置文件,参数值是核心配置文件相对于src的路径
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
//构建连接池
SqlSessionFactory sFactory = new SqlSessionFactoryBuilder().build(reader);
//获取链接对象
SqlSession session = sFactory.openSession();
//调用mapper中的接口 mapper也就是原来的DAO
UserMapper uMapper = session.getMapper(UserMapper.class);
//调用方法
User user = uMapper.getSelectId(1);
System.out.println();
List<User> list = uMapper.selectAll();
for (User user2 : list) {
System.out.println(user2.getPhone());
}
System.out.println(user.getPossword());
五.一对一
<mapper namespace="mybaentity.mapper.OneToOneMapper">
<select id="getUertByName" parameterType="String" resultMap="byname">
select * from user u,city c where u.cityid = c.cityid and username =
#{username}
</select>
<resultMap type="User" id="byname">
<result property="userid" column="userid" />
<result property="username" column="username" />
<result property="possword" column="possword" />
<result property="email" column="email" />
<result property="phone" column="phone" />
<association property="city" column="cityid" javaType="City"
resultMap="cityResult" />
</resultMap>
<resultMap type="City" id="cityResult">
<result property="cityid" column="cityid" />
<result property="cityname" column="cityname" />
</resultMap>
<!-- <select id="getUertByName" parameterType="String" resultMap="byname">
select * from user where username = #{username}
</select>
<resultMap type="User" id="byname">
property:嵌套查询返回的结果赋值给当前类的哪个属性
column:数据库中外检列的名字
javaType:嵌套查询返回的结果集类型
select:要嵌套的查询select元素的id值
<association property="city" column="cityid" javaType="City" select="cityselect"/>
</resultMap>
<select id="cityselect" parameterType="int" resultType="City">
select * from city where cityid = #{cityid}
</select> -->
</mapper>
六.增删改查
<mapper namespace="mybaentity.mapper.UserManagerMapper">
<select id="getUser" parameterType="map" resultType="User">
select * from user where username = #{k_username} and possword = #{k_possword}
</select>
<!-- 添加 -->
<insert id="insertUser">
insert into user(username,possword,email,phone) values(#{username},#{possword},#{email},#{phone})
</insert>
<!-- 删除一个 -->
<delete id="deleteUser">
delete from user where userid = #{userid}
</delete>
<!-- 删除多个 -->
<delete id="deleteUserAll">
delete from user where userid in
<!--
遍历数组或者集合
collection:元素的参数类型
item:条件 (当前遍历的元素)
open:得到( (得到的元素以什么字符开始)
close得到) (得到的元素以什么字符结束)
separator:用,号分割(用什么来分割)
-->
<foreach collection="array" item="userid" open="(" close=")" separator=",">
#{userid}
</foreach>
</delete>
<!-- 修改 -->
<update id="updateUser">
update user set username = #{username},possword = #{possword},email = #{email} where userid = #{userid}
</update>
</mapper>
七.一对多
<mapper namespace="mybaentity.mapper.OneMoreMapper">
<select id="getProvinceByName" parameterType="int" resultMap="pro_mapper">
select * from Province p,city c where p.pr_id=c.pr_id and p.pr_id =
#{p.pr_id}
</select>
<resultMap type="Province" id="pro_mapper">
<!-- 一对多:property:返回集合赋值给当前类的哪个属性
ofType:集合当中每个元素的类型 -->
<result column="pr_id" property="pr_id" />
<result column="pr_name" property="pr_name" />
<collection property="cities" ofType="City">
<result column="cityid" property="cityid"/>
<result column="cityname" property="cityname"/>
</collection>
</resultMap>
</mapper>
八.动态SQL
<mapper namespace="mybaentity.mapper.DynamicSelectMapper">
<select id="get" parameterType="User" resultType="user">
select * from user
<where>
<if test="city.cityid !=null and city.cityid !=0">
and cityid = #{city.cityid}
</if>
<if test="email!=null and email !=''">
and email = #{email}
</if>
<if test="phone!=null and phone !=''">
and phone = #{phone}
</if>
</where>
</select>
</mapper>
网友评论