MyBatis+Spring实现基本CRUD操作
一、MyBaits介绍
MyBatis 是一个可以自定义SQL、存储过程和高级映射的持久层框架。MyBatis 摒除了大部分的JDBC代码、手工设置参数和结果集重获。MyBatis 只使用简单的XML 和注解来配置和映
射基本数据类型、Map 接口和POJO 到数据库记录。相对Hibernate和Apache OJB等“一站式”ORM解决方案而言,Mybatis 是一种“半自动化”的ORM实现。
与hibernate对比,MyBatis更基础,要求使用者自己控制的东西更多。mybatis完成了基本的一些ORM概念,但是没有Hibernate那么完善。要使用mybatis,程序员的关注点更集中于SQL和数据库结构设计。mybatis没有hibernate使用起来那么面向对象,所以,在使用mybatis的时候,hibernate的一些思想和设计需要改变。
二、环境配置
1、引入jar包
2、配置web.xml
<!-- 加载spring的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
3、编写连接数据配置文件 jdbc.properties
4、编写spring.xml配置文件
<!-- 数据库配置文件位置 -->
<context:property-placeholder location="classpath:jdbc.properties" /> <!-- 配置dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置mybitasSqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis.xml"></property>
</bean> <!-- 配置SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean> <!-- 事务配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 使用注解方式 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean> <!-- 配置dao -->
<bean id="userDao" class="com.mybaits.dao.impl.UserDaoImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>
</bean> <!-- 配置service --> <bean id="userService" class="com.mybaits.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
三、CRUD操作
1、编写实体类User
private int id;
private String username;
private String usernumber;
private String loginname;
private String loginpassword;
private String sex;
private Date birthday;
//.......此处省略get/set方法
2、Dao层接口及实现类
public interface IUserDao {
public void addUser(User user);
public void deleteUser(String usernumber);
public void updateUser(User user);
public User findUser(String usernumber);
public List<User> findUser();
}
public class UserDaoImpl implements IUserDao{
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public void addUser(User user) {
// TODO Auto-generated method stub
sqlSessionTemplate.insert("addUser", user);
}
@Override
public void deleteUser(String usernumber) {
// TODO Auto-generated method stub
sqlSessionTemplate.delete("deleteUser",usernumber);
}
//........此处省略其他方法
//sqlSessionTemplate.delete("deleteUser",usernumber);其中"deleteUser"对应user.xml中,delete标签中的id,其他也类似
}
3、创建User对象的sql语句映射文件,user.xml
<mapper namespace="com.mybaits.bean.User">
<resultMap type="com.mybaits.bean.User" id="userResult">
<id property="id" column="id" />
<result property="username" column="username"/>
<result property="usernumber" column="usernumber"/>
<result property="loginname" column="loginname"/>
<result property="loginpassword" column="loginpassword"/>
<result property="sex" column="sex"/>
<result property="birthday" column="birthday" />
</resultMap> <insert id="addUser" parameterType="User" >
insert into user (username,usernumber,loginname,loginpassword,sex,birthday) values(#{username},#{usernumber},#{loginname},#{loginpassword},#{sex},#{birthday})
</insert>
<delete id="deleteUser" parameterType="String">
delete from user where user.usernumber=#{usernumber}
</delete> <update id="updateUser" parameterType="User" >
update user set username=#{username},loginname=#{loginname},loginpassword=#{loginpassword},sex=#{sex},birthday=#{birthday} where usernumber=#{usernumber}
</update>
<select id="findUserByUsernumber" parameterType="string" resultType="User">
select * from user where usernumber = #{usernumber}
</select>
<select id="findAllUser" resultType="User">
select * from user
</select>
<select id="findUserByName" parameterType="String" resultType="User">
select * from user where usernumber like CONCAT(CONCAT('%', #{usernumber}), '%')
</select>
</mapper>
<!-- resultType="User" 对应 <typeAlias type="com.mybaits.bean.User" alias="User"/> 中的User-->
4、创建Mybaits的mapper配置文件mybaits.xml
typeAliases:给类起别名
mappers:加载实体类User的sql映射文件
<configuration>
<typeAliases>
<typeAlias type="com.mybaits.bean.User" alias="User"/>
</typeAliases>
<mappers>
<mapper resource="com/mybaits/dao/impl/user.xml" />
</mappers>
</configuration>
四、使用junit编写测试类
public class UserTest {
private IUserService userService;
private IUserDao userDao;
@Before
public void init(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
userService=(IUserService) ctx.getBean("userService");
userDao=(IUserDao) ctx.getBean("userDao");
}
@Test
public void Test(){
User u=new User();
u.setUsername("王一飞");
u.setUsernumber("01111305");
u.setLoginname("admin");
u.setLoginpassword("admin");
u.setSex("男");
u.setBirthday(new Date());
userDao.addUser(u);
}
@Test
public void Test3(){
User u=userService.findUser("01111001");
System.out.println(u.getUsername());
}
//.....此处省略其他测试方法
}
MyBatis+Spring实现基本CRUD操作的更多相关文章
- mybatis升级案例之CRUD操作
mybatis升级案例之CRUD操作 一.准备工作 1.新建maven工程,和入门案例一样 主要步骤如下,可参考mybatis入门实例 a.配置pom.xml文件 b.新建实例类User.DAO接口类 ...
- SSM框架之Mybatis(2)CRUD操作
Mybatis(2)CRUD 1.基于代理Dao实现CRUD操作 使用要求: 1.持久层接口(src\main\java\dao\IUserDao.java)和持久层接口的映射配置(src\main\ ...
- MyBatis 对数据库进行CRUD操作
1.update修改 uodate修改也可以使用之前的机制在配置文件中直接编写sql 但是update语句的set字句中是根据传入的值决定的, 此时可以通过Mybatis提供的标签实现判断动态拼接up ...
- spring jdbcTemplate的CRUD操作
一.jdbcTemplate准备 1.导入与jdbcTemplate相关的jar包 2.设置数据库信息 3.创建jdbcTemplate对象,设置数据源 二.添加操作 1.代码 2.结果 三.修改操作 ...
- mybatis学习:mybatis的注解开发CRUD操作
Dao层: public interface IUserDao { /** * 查询所有结果 * @return */ @Select("select * from user") ...
- Spring Boot整合Mybatis并完成CRUD操作
MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...
- MyBatis学习存档(4)——进行CRUD操作
使用MyBatis进行数据库的CRUD操作有2种方式:一种如之前所说的接口+xml,而另一种是通过对接口上的方法加注解(@Select @Insert @Delete @Update) 但是通常情况下 ...
- Spring Boot整合Mybatis完成级联一对多CRUD操作
在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...
- Spring boot 入门四:spring boot 整合mybatis 实现CRUD操作
开发环境延续上一节的开发环境这里不再做介绍 添加mybatis依赖 <dependency> <groupId>org.mybatis.spring.boot</grou ...
随机推荐
- 编译net core时nuget里全部报错,\obj\project.assets.json找不到
除了Nuget管理设置允许下载缺少的程序包 如果你自己设置的程序包源里有一个源访问不到,则可能出现下面错误,导致所有nuget无法还原. 而且在VS2017里不会出现这个 SDK,特别是你网上下载的其 ...
- Develop Android Game Using Cocos2d-x
0. Environment Windows 7 x64Visual Studio 2013adt-bundle-windows-x86 (http://developer.android.com/s ...
- ssh以bash登录的配置
因ssh登录时不会加载.bashrc而是加载.bash_profile,所以以ssh的默认登录不会是bash,只要在.bash_profile中添加以下代码即可: if [ -f ~/.bashrc ...
- [Node] Agenda 中文文档 定时任务调度系统[基础篇]
Agenda简介 使用步骤概述 步骤详述 初始化 定义任务 参数说明: 配置任务 参数说明 设置监听 注意事项 Agenda简介 Agenda是一个定时任务管理模块,它将node中的定时任务存储在数据 ...
- 在 C/C++ 中使用 TensorFlow 预训练好的模型—— 直接调用 C++ 接口实现
现在的深度学习框架一般都是基于 Python 来实现,构建.训练.保存和调用模型都可以很容易地在 Python 下完成.但有时候,我们在实际应用这些模型的时候可能需要在其他编程语言下进行,本文将通过直 ...
- 分词(Tokenization) - NLP学习(1)
自从开始使用Python做深度学习的相关项目时,大部分时候或者说基本都是在研究图像处理与分析方面,但是找工作反而碰到了很多关于自然语言处理(natural language processing: N ...
- 用IIS防止mdb数据库被下载(转载)
原网址:http://www.cnblogs.com/kingreatwill/p/4224433.html 第一种方法:要求网站管理人员具体asp编程经验.因为现在的销售虚拟主机的系统,已经为用户建 ...
- 【iOS开发】IOS界面开发使用viewWithTag:(int)findTag方法获取界面元素
http://blog.csdn.net/lxp1021/article/details/43952551 今天在开发OS界面的时候,遇到通过界面UIview viewWithTag:(int)fin ...
- [剑指Offer] 18.二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5 [思路1 ...
- 并发(一) Semaphore
Semaphore 控制对资源的并发访问数,构造时如果传参为1,则近似于ReentrantLock,差别在于锁的释放.可以一个线程获取锁,另外一个线程释放锁,在一些死锁处理的场合比较适用. 如上所示, ...