`

二、mybatis的CRUD-基于接口的方式

阅读更多
1.直接调用SQL语句,容易写错它的全限定名(命名空间+SQL语句id),写错后IDE也不能给出提示。
2.更好的方式是定义与SQL映射文件对应的接口(接口的全限定名,即包名+类名,与邪社文件的namespace相同),在接口中声明与SQL语句对应的方法(方法的名字、蚕食的类型和返回类型与SQL语句的id、parameterType以及返回类型一致),也就是把方法与SQL语句绑定起来
3.然后通过接口的对象(通过SqlSession获得)来调用这些方法,实际上最终还是调用这些SQL语句。这样就能避免输入很长的SQL语句全限定名,在调用方法是还能利用IDE的代码自动补全功能,在写错方法时IDE也能给出提示



一.创建工程(同上一章)
[img]

[/img]

此目录多了dao接口
二.创建实体
private int id;
private String name;
private String password;
private String telPhone;
private String email;

set、get......

三.mybatis配置文件
<?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,settings,typeAliases,typeHandlers,objectFactory,objectWrapperFactory,plugins,environments,databaseIdProvider,mappers
	 -->
	 <!-- 方式一 -->
	 <!-- 
	<typeAliases>
		<typeAlias type="com.yxhweb.entity.User" alias="user" />
	</typeAliases>
	 -->
	<!-- 方式二(开发使用) -->
	<typeAliases>
		<package name="com.yxhweb.entity"/>
	</typeAliases>
	
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3308/db_mybatis" />
				<property name="username" value="root" />
				<property name="password" value="root" />
			</dataSource>
		</environment>
	</environments>
	
	<mappers>
		<mapper resource="com/yxhweb/mappers/UserMapper.xml" />
		<!-- <package name="com.yxhweb.mappers" /> -->
	</mappers>
	
</configuration>



四.编写mybatis实现
<?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.yxhweb.dao.UserDao">
	<!-- 添加 -->
	<insert id="add" useGeneratedKeys="true" keyColumn="id" keyProperty="id" parameterType="user">
		INSERT INTO t_user(NAME,PASSWORD,telPhone,email) VALUES(#{name},#{password},#{telPhone},#{email})
	</insert>
	<!-- 删除 -->
	<insert id="delete" parameterType="int" >
		delete from t_user where id=#{id}
	</insert>
	<!-- 根据id查询 -->
	<select id="getById" parameterType="int"  resultType="user">
		select id,name,password,telPhone,email from t_user where id=#{id}
	</select>
	<!-- 修改 -->
	<update id="update" parameterType="user">
		update t_user set name=#{name},password=#{password},telPhone=#{telPhone},email=#{email} where id=#{id}
	</update>
	
	<!-- 查询 全部-->
	<resultMap  id="UserResultMap" type="user">
		<id property="id" column="id"/>	 
		<result property="name" column="name"/>
		<result property="password" column="password"/>
		<result property="telPhone" column="telPhone"/>
		<result property="email" column="email"/>
	 </resultMap>
	 
	 <select id="getAll" parameterType="int"  resultType="UserResultMap">
		select id,name,password,telPhone,email from t_user
	</select>
	
</mapper>


此文件和上一章的区别
1.namespace:com.yxhweb.dao.UserDao对应接口路径
2.sql 的id对应接口中的方法

源码地址:链接:http://pan.baidu.com/s/1gevmEBL 密码:raao
  • 大小: 16.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics