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 resource="db.properties"/>
<typeAliases>
<package name="com.model"/>
</typeAliases>
<environments default="test">
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${jdbcUrl}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments> <mappers>
<package name="com.dao"/><!-- 自动装配包里的接口 -->
</mappers> </configuration>
driver=oracle.jdbc.OracleDriver
jdbcUrl=jdbc:oracle:thin:@localhost::orcl
username=test1116
password=
数据库
两个实体类
package com.model; import java.util.Date; public class Student {
private Integer sno;
private String sname;
private String ssex;
private Integer sclass; public Student() {
super();
} public Student(Integer sno, String sname, String ssex, Integer sclass) {
super();
this.sno = sno;
this.sname = sname;
this.ssex = ssex;
this.sclass = sclass;
} public Integer getSno() {
return sno;
}
public void setSno(Integer sno) {
this.sno = sno;
} public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
} @Override
public String toString() {
return "Student [sno=" + sno + ", sname=" + sname + ", ssex=" + ssex + ", sclass=" + sclass + "]";
} public Integer getSclass() {
return sclass;
}
public void setSclass(Integer sclass) {
this.sclass = sclass;
} }
package com.model; import java.util.Date; public class StudentInfo {
private Integer id;
private Student student;
private String saddress;
private Date sbirthday; public StudentInfo() {
super();
} public StudentInfo(Integer id, Student student, String saddress, Date sbirthday) {
super();
this.id = id;
this.student = student;
this.saddress = saddress;
this.sbirthday = sbirthday;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String getSaddress() {
return saddress;
}
public void setSaddress(String saddress) {
this.saddress = saddress;
}
public Date getSbirthday() {
return sbirthday;
}
public void setSbirthday(Date sbirthday) {
this.sbirthday = sbirthday;
}
@Override
public String toString() {
return "StudentInfo [id=" + id + ", student=" + student + ", saddress=" + saddress + ", sbirthday=" + sbirthday
+ "]";
}
}
两个接口
package com.dao; import java.util.List;
import java.util.Map; import com.model.Student; public interface StudentMapper {
/**
* 添加
*/
public Integer add(Student student);
/**
* 删除
*/
public Integer delete(Integer son);
/**
* 修改
*/
public Integer update(Student student);
/**
* 根据主键查查询
*/
public Student select(Integer sno);
/**
* 全表查询
*/
public List<Student> selectall();
/**
* 多条件查询
*/
public List<Student> selectduo(Map<String, Object> map);
}
package com.dao; import java.util.List; import com.model.StudentInfo; public interface StudentInfoMapper {
/**
* 一对一关联查询
* @return
*/
public List<StudentInfo> selectall();
}
对应的两个配置文件,注意接口名要跟实配置文件名字一样,相当与实现了接口
<?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.dao.StudentMapper">
<!-- 添加 -->
<insert id="add" parameterType="student">
insert into student values(#{sno},#{sname},#{ssex},#{sclass})
</insert>
<!-- 删除 -->
<delete id="delete" parameterType="Integer">
delete student s where s.sno=#{sno}
</delete>
<!-- 修改 -->
<update id="update" parameterType="student">
update student s set s.sname=#{sname},s.ssex=#{ssex},s.sclass=#{sclass} where s.sno=#{sno}
</update>
<!-- 根据主键查 -->
<select id="select" parameterType="Integer" resultType="student">
select * from student s where s.sno=#{sno}
</select>
<!-- 查全部 -->
<select id="selectall" resultType="student" >
select * from student
</select>
<!-- 多条件查询 -->
<select id="selectduo" parameterType="Map" resultType="student" >
select * from student s where s.sno=#{sno} and s.sname like #{sname}
</select>
</mapper>
<?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.dao.StudentInfoMapper">
<!-- 一对一关联查询 -->
<resultMap type="studentInfo" id="silist">
<result property="id" column="id"/> <result property="student.sno" column="sno"/>
<result property="student.sname" column="sname"/>
<result property="student.ssex" column="ssex"/>
<result property="student.sclass" column="sclass"/> <result property="saddress" column="saddress"/>
<result property="sbirthday" column="sbirthday"/>
</resultMap>
<!-- 一对一另一种方法 -->
<resultMap type="StudentInfo" id="silist1">
<association property="student" column="sno" select="com.dao.StudentMapper.select"/>
</resultMap> <select id="selectall" resultMap="silist1">
select * from studentinfo si
left join student s on si.sno=s.sno
</select> </mapper>
sqlsession工具类
package com.util; import java.io.IOException;
import java.io.InputStream; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class SqlSessionUtil {
private static SqlSessionFactory ssf;
private static SqlSession ss; public static SqlSessionFactory getSqlSessionFactory(){
InputStream in=null;
try {
in=Resources.getResourceAsStream("mybatis-cfg.xml");
ssf=new SqlSessionFactoryBuilder().build(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return ssf;
}
public static SqlSession getSqlSession(){
ss=getSqlSessionFactory().openSession();
return ss;
}
public static void main(String[] args) {
System.out.println(getSqlSession());
} }
第一个junit测试
package com.util; import static org.junit.Assert.*; import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.dao.StudentMapper;
import com.model.Student; public class Jtest {
private SqlSession ss;
private StudentMapper sm;
@Before
public void setUp() throws Exception {
ss=SqlSessionUtil.getSqlSession();
sm=ss.getMapper(StudentMapper.class); } @After
public void tearDown() throws Exception {
ss.commit();
ss.close();
} public void test() {
Student st=new Student(6, "蒙蒙", "男", 95031);
int m=sm.add(st);
System.out.println(m);
}
public void delete() {
int m=sm.delete(6);
System.out.println(m);
}
public void update() {
Student st=new Student(666, "张三", "男", 95033);
int m=sm.update(st);
System.out.println(m);
}
public void select() {
Student st=sm.select(3);
System.out.println(st);
}
public void selectall() {
List<Student> st=sm.selectall();
for(Student tt:st){
System.out.println(tt);
}
}
@Test
public void selectduo() {
Map<String, Object> map=new HashMap<String, Object>();
map.put("sno", 666);
map.put("sname", "%张%");
List<Student> st=sm.selectduo(map);
for(Student tt:st){
System.out.println(tt);
}
} }
第二个junit测试
package com.util; import static org.junit.Assert.*; import java.util.List; import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.dao.StudentInfoMapper;
import com.dao.StudentMapper;
import com.model.StudentInfo; public class JJtest {
private SqlSession ss;
private StudentInfoMapper sim;
@Before
public void setUp() throws Exception {
ss=SqlSessionUtil.getSqlSession();
sim=ss.getMapper(StudentInfoMapper.class); } @After
public void tearDown() throws Exception {
ss.commit();
ss.close();
}
@Test
public void test() {
List<StudentInfo> list=sim.selectall();
for(StudentInfo a:list){
System.out.println(a);
}
} }
多条件查询结果,其它的就不发了
mybatis 使用接口增删改查和两表一对一关联查询的更多相关文章
- MyBatis -- 对表进行增删改查(基于注解的实现)
1.MyBatis对数据库表进行增/删/改/查 前一篇使用基于XML的方式实现对数据库的增/删/改/查 以下我们来看怎么使用注解的方式实现对数据库表的增/删/改/查 1.1 首先须要定义映射sql的 ...
- Spring Boot 使用Mybatis注解开发增删改查
使用逆向工程是遇到的错误 错误描述 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): c ...
- Mybatis实现简单增删改查
Mybatis的简单应用 学习内容: 需求 环境准备 代码 总结: 学习内容: 需求 使用Mybatis实现简单增删改查(以下是在IDEA中实现的,其他开发工具中,代码一样) jar 包下载:http ...
- MyBatis简单的增删改查以及简单的分页查询实现
MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...
- Mybatis入门之增删改查
Mybatis入门之增删改查 Mybatis如果操作成功,但是数据库没有更新那就是得添加事务了.(增删改都要添加)----- 浪费了我40多分钟怀疑人生后来去百度... 导入包: 引入配置文件: sq ...
- SQL Server -- 回忆笔记(二):增删改查,修改表结构,约束,关键字使用,函数,多表联合查询
SQL Server知识点回忆篇(二):增删改查,修改表结构,约束,关键字使用,函数,多表联合查询 1. insert 如果sql server设置的排序规则不是简体中文,必须在简体中文字符串前加N, ...
- TESTUSERB 仅能对TESTUSERA 用户下的某些表增删改查、有些表仅能对某些列update,查询TESTUSERB 用户权限,获取批量赋予语句。
TESTUSERB 仅能对TESTUSERA 用户下的某些表增删改查.有些表仅能对某些列update,查询TESTUSERB 用户权限,获取批量赋予语句. select 'grant '|| PRIV ...
- SpringMVC,MyBatis商品的增删改查
一.需求 商品的增删改查 二.工程结构 三.代码 1.Mapper层 (1) ItemsMapperCustom.java package com.tony.ssm.mapper; import ja ...
- mybatis中的增删改查操作
在这一个部分,主要进行增删改查的示例书写. 增删改查可以基于xml的,也可以基于注解的方式. 一:对单条数据的查询 1.目录结构 这个使得目录更加清晰 2.User.java 这个使用以前的user表 ...
随机推荐
- datetime时间处理
基本数据获取: In [38]: import datetime as dt In [39]: on = dt.datetime.now() #获取当前准确时间 In [40]: on Out[40] ...
- Delphi 正则表达式起步
Delphi 正则表达式起步 在 Delphi 中使用正则表达式, 目前 PerlRegEx 应该是首选, 准备彻底而细致地研究它. 官方网站: http://www.regular-expressi ...
- kubernetes1.9 手动安装
一.创建TLS证书和秘钥: 1.安装 CFSSL: wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 chmod +x cfssl_linux-amd ...
- 【持续更新】ultraedit工具使用总结
常用设置及快捷键 设置Ultraedit自动换行: 永久修改: 点击菜单栏的“高级→配置”,找到“编辑器→自动换行/制表符设置”,选择“默认为每个文件启用自动换行”,ok. 临时修改: 快捷键 Ctr ...
- Silverlight中获取控件中子控件
如题:,直接来看代码: /// <summary> /// 查找并返回第一个 相同 name的子元素 /// </summary> /// <typeparam name ...
- 【leetcode刷题笔记】Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- css盒模型不同浏览器下解释不同 解决办法
盒子模型是css中一个重要的概念,理解了盒子模型才能更好的排版.其实盒子模型有两种,分别是 ie 盒子模型和标准 w3c 盒子模型.他们对盒子模型的解释各不相同,先来看看我们熟知的标准盒子模型: 从上 ...
- 微信小程序获取用户信息,解密encryptedData 包括敏感数据在内的完整用户信息的加密数据
package com.iups.wx.wxservice; import java.io.UnsupportedEncodingException; import java.security.Alg ...
- Spring注解(环境)
以数据库为例: 引入 c3p0数据源maven坐标 数据库驱动 @Configuration @PropertySource("classpath:/db.config.properties ...
- Difference between RouteTable.Routes and HttpConfiguration.Routes?
https://stackoverflow.com/questions/12533782/difference-between-routetable-routes-and-httpconfigurat ...