本文基于下述教程编写:【B站】ssm教程
MyBatis基本CRUD操作
就常用的配置文件
IUserDao.xml
代理实现DAO实现类方法来讲:
<?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.ssmTest.dao.IUserDao">
<!--配置查询所有-->
<select id="findAll" resultType="com.ssmTest.domain.User">
select * from user
</select>
<!--查询具体ID USER-->
<select id="findById" resultType="com.ssmTest.domain.User" parameterType="int">
select * from user where id =#{id}
</select>
<!--添加USER-->
<insert id="saveUser" parameterType="com.ssmTest.domain.User">
<!-- 对应实体类 对应数据库 返回结果类型 执行时间-->
<selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
select last_insert_id();
</selectKey>
insert into user(username,birthday,sex,address)
values(#{username},#{birthday},#{sex},#{address})
</insert>
<!--更新USER-->
<update id="updateUser" parameterType="com.ssmTest.domain.User">
update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}
</update>
<!--删除USER-->
<delete id="deleteUser" parameterType="int">
delete from user where id = #{id}
</delete>
<select id="findByName" resultType="com.ssmTest.domain.User" parameterType="String">
select * from user where username like #{username}
</select>
<select id="findTotal" resultType="int">
select count(*) from user
</select>
</mapper>
#{}中参数类型
在配置文件<mapper>
标签内,增删查改四种操作都有对应的标签名,id
属性必须与Dao接口方法名字一致,需要传参的地方指定parameterType
、需要返回值要指定resultType
的值类型。[注意:参数如果是基本数据类型,#{}
内可以随意起名,resultType/parameterType
中也可以直接写基本类型,若是javabean
就不能,除了在resultType/parameterType
中指定全限定类名,#{}还必须与成员属性名一致]。
返回自增长id
同时,因为数据库内表id
是自动增长的,插入数据后数据库后续才指定id值,在插入语句中设置selectKey
标签可将数据库新生成的id
值拿回来设置到javabean
中。
resultMap结果类型
问题来了,如果出现实体类的属性名和数据库字段名不一致的情况,怎么解决映射问题?
resultMap
标签可以建立查询的列名和实体类的属性名称不一致时建立对应关系。从而实现封装。 在
select
标签中使用
resultMap
属性指定引用即可。同时
resultMap
可以实现将查询结果映射为复杂类型的
pojo
,比如在查询结果映射对象中包括
pojo
和
list
实现一对一查询和一对多查询。
<?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.ssmTest.dao.IUserDao">
<!--id 标签:用于指定主键字段 result 标签:用于指定非主键字段
column 属性:用于指定数据库列名 property 属性:用于指定实体类属性名称-->
<resultMap type="com.itheima.domain.User" id="userMap">
<id column="id" property="userId"/>
<result column="username" property="userName"/>
<result column="sex" property="userSex"/>
<result column="address" property="userAddress"/>
<result column="birthday" property="userBirthday"/>
</resultMap>
<!--配置查询所有-->
<select id="findAll" resultMap="userMap">
select * from user
</select>
</mapper>
junit集成测试
集成依赖:
<!-- 单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
这个东西的最明显作用就是,在代码中可以不用写main
方法,只需要打一个注解,选中方法名右键run
就可以。
@Before
:在所有测试方法前调用。
@After
:在所有测试方法后调用。
@Test
:测试方法,单独调用。
@Before//在测试方法前统一调用
public void init() throws IOException {
//1.读取配置文件
in= Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建SqlSessionFactory
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
factory=builder.build(in);
//MyBtis实现处理层Impl方式
//UserDaoImpl userDao=new UserDaoImpl(factory);
//3.使用工厂生产SqlSession对象
sqlSession = factory.openSession();
//4.使用SqlSession创建Dao接口的代理对象
userDao=sqlSession.getMapper(IUserDao.class);
}
//查询具体ID对应User
@Test
public void testFindOne(){
User user=userDao.findById(42);
System.out.println(user);
}
@After//在测试方法后统一调用
public void destroy() throws IOException {
sqlSession.commit();
sqlSession.close();
in.close();
}
主配置文件数据库连接信息解耦
数据库配置信息解耦
<properties>
标签可以将数据库连接信息给提取出来,放到该标签下,亦可以提取到resource
文件夹下面,作为外部问文件引用。
将信息直接写在主配置文件中:
外部文件引用方式:
jdbcConfig.properties
:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/eesy_mybatis
jdbc.username=root
jdbc.password=1234
主配置文件中:
<properties>
标签配置resource
属性:用于指定 properties
配置文件的位置,要求配置文件必须在类资源路径resource文件夹下。
<properties>
标签也可以配置url
属性指定 properties
配置文件的位置,此时不必拘束于应用资源文件夹内:
URL: Uniform Resource Locator
统一资源定位符 如:http://localhost:8080/mystroe/CategoryServlet,
由URL/file协议 +主机+端口+路径,四部分组成。
URI:Uniform Resource Identifier
统一资源标识符 如:/mystroe/CategoryServlet ,它是可以在 web 应用中唯一定位一个资源的路径
主配置文件实体类、接口起别名
实体类起别名:
<typeAliases/>
:可以指定新的名字作为实体类名的别名,不区分大小写被引用在接口配置文件的
type
属性中。
在
SqlMapConfig.xml
中配置:
<typeAliases>
<!-- 单个别名定义 -->
<typeAlias alias="user" type="com.itheima.domain.User"/>
<!-- 批量别名定义,扫描整个包下的类,别名为类名(首字母大写或小写都可以) -->
<package name="com.itheima.domain"/>
<package name=" 其它包 "/>
</typeAliases>
在 Userdao.xml
中配置:
<!--配置查询所有-->
<select id="findAll" resultType="UsEr">
select * from user
</select>
接口映射三个属性
<mapper resource=" " />
使用相对于类路径的资源 如:<mapper resource="com/itheima/dao/IUserDao.xml" />
<mapper class=" " />
使用 mapper 接口类路径 如:<mapper class="com.itheima.dao.UserDao"/>
注意:此种方法要求 mapper 接口名称和 mapper 映射文件名称相同,且放在同一个目录中。
<package name=""/>
注册指定包下的所有 mapper 接口,此时不再需要指定上述<mapper>
标签了, 如:<package name="com.itcast.dao"/>
注意:此种方法要求 mapper 接口名称和 mapper 映射文件名称相同,且放在同一个目录中。