mytabis

编程入门 行业动态 更新时间:2024-10-05 19:18:19

mytabis

mytabis

  1. 查询所有学生数据,主外键关联查询
  • 项目结构

  • 数据库
-- Table structure for `class`
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (`cid` int(50) NOT NULL AUTO_INCREMENT,`cname` varchar(50) COLLATE utf8_bin NOT NULL,PRIMARY KEY (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;-- ----------------------------
-- Records of class
-- ----------------------------
INSERT INTO `class` VALUES ('1', '1001');
INSERT INTO `class` VALUES ('2', '1002');
INSERT INTO `class` VALUES ('3', '1003');
INSERT INTO `class` VALUES ('4', '1004');-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (`sid` int(50) NOT NULL AUTO_INCREMENT,`sname` varchar(50) COLLATE utf8_bin NOT NULL,`ssex` varchar(50) COLLATE utf8_bin NOT NULL,`sage` varchar(50) COLLATE utf8_bin NOT NULL,`scid` int(50) NOT NULL,PRIMARY KEY (`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', '星  星', '男', '20', '1');
INSERT INTO `student` VALUES ('2', '猪八戒', '男', '33', '3');
INSERT INTO `student` VALUES ('3', '施耐庵', '男', '37', '2');
INSERT INTO `student` VALUES ('4', '约翰逊', '男', '21', '2');
INSERT INTO `student` VALUES ('5', '周恩来', '男', '32', '2');
INSERT INTO `student` VALUES ('6', '小  小', '女', '18', '1');
INSERT INTO `student` VALUES ('7', '高曼君', '女', '25', '4');
INSERT INTO `student` VALUES ('8', '胡  适', '男', '27', '3');
INSERT INTO `student` VALUES ('9', '胡夫人', '女', '25', '4');
INSERT INTO `student` VALUES ('12', '懒羊羊', '男', '17', '3');
INSERT INTO `student` VALUES ('13', '暖洋洋', '女', '18', '3');
INSERT INTO `student` VALUES ('15', '美羊羊', '女', '18', '3');
  •  MybatisUtils
package com.xiao.util;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;
import java.io.InputStream;public class MybatisUtils {//加载mybatis配置文件//创建sqlsessionfactory对象//创建开始sqlsession连接对象//创建关闭sqlsession连接对象private static SqlSessionFactory sessionFactory;static{String resource="mybatis.xml";try {InputStream is= Resources.getResourceAsStream(resource);sessionFactory=new SqlSessionFactoryBuilder().build(is);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static SqlSession getSession(){return sessionFactory.openSession();}public static void closeSession(SqlSession session){if(session!=null){session.close();}}}
  • mytais.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis//DTD Config 3.0//EN"
".dtd">
<configuration><typeAliases><package name="com.xiao.entity"/></typeAliases><!-- 配置环境 --><environments default="development"><environment id="development"><!-- 使用JDBC的事务管理 --><transactionManager type="JDBC"/><!-- type="POOLED"它是固定值 --><dataSource type="POOLED"><!-- MySQL数据库驱动 --><property name="driver" value="com.mysql.cj.jdbc.Driver"/><!-- 连接数据库的URL因为我的数据库是MySQL8,但idea版本低,所以在驱动和路径配置时不太一样useSSL=false&amp;serverTimezone=UTC--><property name="url" value="jdbc:mysql://localhost:3306/homework?useSSL=false&amp;serverTimezone=UTC"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments><mappers><!-- 映射文件的位置 --><mapper resource="com/xiao/mapper/StudentMapper.xml"/></mappers>
</configuration>
  • StudentInfo(实体类)
package com.xiao.entity;public class StudentInfo {private int sid;private String sname;private String ssex;private String sage;private int scid;private String cname;public int getSid() {return sid;}public void setSid(int sid) {this.sid = sid;}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;}public String getSage() {return sage;}public void setSage(String sage) {this.sage = sage;}public int getScid() {return scid;}public void setScid(int scid) {this.scid = scid;}public String getCname() {return cname;}public void setCname(String cname) {thisame = cname;}public StudentInfo() {}public StudentInfo(int sid, String sname, String ssex, String sage, int scid, String cname) {this.sid = sid;this.sname = sname;this.ssex = ssex;this.sage = sage;this.scid = scid;thisame = cname;}public StudentInfo(String sname, String ssex, String sage, int scid, String cname) {this.sname = sname;this.ssex = ssex;this.sage = sage;this.scid = scid;thisame = cname;}public StudentInfo(String sname, String ssex, String sage, int scid) {this.sname = sname;this.ssex = ssex;this.sage = sage;this.scid = scid;}
}
  • StudentMapper接口
package com.xiao.mapper;import com.xiao.entity.StudentInfo;
import org.apache.ibatis.annotations.Param;import java.util.List;
import java.util.Map;public interface StudentMapper {//    查询所有的学生public List<StudentInfo> findAll();
}
  • StudentMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis//DTD Config 3.0//EN"".dtd">
<mapper namespace="com.xiao.mapper.StudentMapper"><resultMap id="studentlist" type="StudentInfo"><result property="sid" column="sid"/><result property="sname" column="sname"/><result property="ssex" column="ssex"/><result property="sage" column="sage"/><result property="scid" column="scid"/><result property="cname" column="cname"/></resultMap><!--查询所有的学生--><select id="findAll" resultMap="studentlist">SELECT student.*,classame FROM student ,class  WHERE student.scid= class.cid</select></mapper>   
  • Test
package com.xiao.test;import com.xiao.entity.StudentInfo;
import com.xiao.mapper.StudentMapper;
import com.xiao.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;import java.util.HashMap;
import java.util.List;public class Test {public static void main(String[] args) {test01();}//    查询所有的学生private static void test01() {SqlSession session = MybatisUtils.getSession();List<StudentInfo> allStu = session.getMapper(StudentMapper.class).findAll();for (StudentInfo s : allStu) {System.out.println(s.getSid() + "\t" + s.getSname() + "\t" + s.getCname() + "\t");}}
  • 运行效果

 

更多推荐

mytabis

本文发布于:2024-02-28 04:26:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1767910.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:mytabis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!