mybatis解析--转载
来源:互联网 发布:matlab矩阵元素运算 编辑:程序博客网 时间:2024/06/09 22:16
转载http://www.cnblogs.com/wushiqi54719880/archive/2011/07/26/2117601.html
本文主要介绍了如何使用mybatis进行简单的数据库操作。本人使用的是mybatis3.05。
Mybatics 是apache 开源项目的一个数据持久层框架,其包括sql maper(sql 映射)以及 Data access object的内容。我们将通过以下示例来讲解mybatics 的相关内容。
1.创建数据库表(User表)
CREATE TABLE `NewTable` (`userId` bigint(20) NOT NULL AUTO_INCREMENT ,`userName` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,`password` varchar(80) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,`comment` varchar(250) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,PRIMARY KEY (`userId`),UNIQUE INDEX `UQ_User_userName` (`userName`) USING BTREE)
2.2. 创建实体类
package com.mybatis.config;public class User { private int userId; private String userName; private String password; private String comment;
// get set 方法 public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; }}
3. 定义数据访问接口
package com.mybatis.config;import java.util.List;public interface UserDao {
// 对数据的访问主要为 增删改查 public int insert(User user); public int update(User user); public int delete(String userName); public List<User> selectAll(); public int countAll(); public User findByUserName(String userName);}
4.创建MyBatis映射文件(UserDaoMapper.xml)--数据访问接口映射文件,在xml文件中实现
<?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.mybatis.config.UserDao"> <select id="countAll" resultType="int"> select count(*) c from user; </select> <select id="selectAll" resultType="com.mybatis.config.User"> select * from user order by userName asc </select> <insert id="insert" parameterType="com.mybatis.config.User"> insert into user(userName,password,comment) values(#{userName},#{password},#{comment}) </insert> <update id="update" parameterType="com.mybatis.config.User"> update user set userName=#{userName},password=#{password},comment=#{comment} where userName=#{userName} </update> <delete id="delete" parameterType="int"> delete from user where userName=#{userName} </delete> <select id="findByUserName" parameterType="String" resultType="com.mybatis.config.User"> select * from user where userName=#{userName} </select></mapper>
5. 创建mybatis配置文件--- 配置文件1,写入数据访问映射文件的路径 ,userMapperDao.xml 文件
9. 编写测试代码
package com.mybatis;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserServiceTest {
@Test
public void userServiceTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService)context.getBean("userService");
System.out.println(userService.countAll());
}
}
- mybatis解析--转载
- Mybatis实现原理深入解析 (转载)
- Mybatis实现原理深入解析 (转载)
- mybatis入门基础(三)----SqlMapConfig.xml全局配置文件解析------------转载
- mybatis转载
- Mybatis 配置文件(转载)
- 转载:mybatis入门
- 【转载】MyBatis入门
- 【转载】MyBatis学习
- mybatis 一对多 解析
- MyBatis的配置文件解析
- mybatis generator 配置文件解析
- Mybatis resultMap解析
- mybatis 参数解析
- MyBatis原理--配置解析
- mybatis原理解析
- Mybatis传多个参数解析
- mybatis foreach 解析
- uva 375 - Inscribed Circles and Isosceles Triangles
- wince中重启网卡
- 程序的内存布局篇
- 压缩感知进阶——有关稀疏矩阵
- VS2008 C# 检测到无法访问的代码的解决方法
- mybatis解析--转载
- Android源码编译整理总结
- 网卡MAC地址相关信息大全(整理)(下)
- Jquery三层实现删除功能
- 1128 分解质因数
- Qt状态机学习2
- 网卡MAC地址相关信息大全(整理)(上)
- 不要轻易受别人的影响
- 【转载】该用 Abstract Class 还是 Interface?