博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
九、Spring集成Mybatis
阅读量:4072 次
发布时间:2019-05-25

本文共 3250 字,大约阅读时间需要 10 分钟。

9.1添加jar包

9.2新建数据表

CREATE TABLE s_user(user_id INT AUTO_INCREMENT PRIMARY KEY,user_name VARCHAR(30),user_birthday DATE,user_salary DOUBLE)

9.3新建实体类

User.java

package com.domain;import java.util.Date;public class User {
private int id; private String name; private Date birthday; private double salary; public User() { super(); } public User(int id, String name, Date birthday, double salary) { super(); this.id = id; this.name = name; this.birthday = birthday; this.salary = salary; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", birthday=" + birthday + ", salary=" + salary + "]"; } }

9.4 DAO 接口: UserMapper

UserMapper.java

package com.mapper;import java.util.List;import com.domain.User;public interface UserMapper {
void save(User user); void update(User user); void delete(int id); User findById(int id); List
findAll();}

9.5 SQL 映射文件: userMapper.xml

userMapper.xml

insert into s_user(user_name,user_birthday,user_salary) values(#{name},#{birthday},#{salary})
update s_user set user_name=#{name},user_birthday=#{birthday},user_salary=#{salary} where user_id=#{id}
delete from s_user where user_id=#{id}

9.6 spring 的配置文件: beans.xml

beans.xml

9.7 mybatis 的配置文件: mybatis-config.xml

mybatis-config.xml

9.8 测试类

SMTest.java

package com.test;import java.util.Date;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.domain.User;import com.mapper.UserMapper;@RunWith(SpringJUnit4ClassRunner.class)//使用spring的测试框架@ContextConfiguration("/beans.xml") //加载spring的beans.xml配置文件 public class SMTest {
@Autowired private UserMapper userMapper; //测试添加 @Test public void testAdd() { userMapper.save(new User(-1,"Tom",new Date(),3000)); } //测试update @Test public void testUpdate() { userMapper.update(new User(1,"Tom2",new Date(),3000)); }}

转载地址:http://hfkni.baihongyu.com/

你可能感兴趣的文章
Encoding Schemes
查看>>
带WiringPi库的交叉笔译如何处理二之软链接概念
查看>>
Java8 HashMap集合解析
查看>>
自定义 select 下拉框 多选插件
查看>>
linux和windows内存布局验证
查看>>
Linux常用统计命令之wc
查看>>
fastcgi_param 详解
查看>>
搞定Java面试中的数据结构问题
查看>>
React Native(一):搭建开发环境、出Hello World
查看>>
React Native(二):属性、状态
查看>>
JSX使用总结
查看>>
React Native(四):布局(使用Flexbox)
查看>>
React Native(七):Android双击Back键退出应用
查看>>
Android自定义apk名称、版本号自增
查看>>
【剑指offer】q50:树中结点的最近祖先
查看>>
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Candy(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>