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

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


一.创建工程(同上一章)
[img]
[img]http://dl2.iteye.com/upload/attachment/0114/4208/c264e58c-0253-3f1c-a360-7ae24792ccea.png[/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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值