1.创建数据库对应的实体类
News 实体类
package com.entity;
import lombok.Data;
import java.util.Date;
import java.util.List;
/**
* @author 弃
* @data 2021/12/11 20:47
*/
@Data
public class News {
private int nid;
private int ntid;
private String ntitle;
private String nauthor;
private Date ncreateDate;
private String npicPath;
private String ncontent;
private Date nmodifyDate;
private String nsummary;
private List<Comment> comments;
}
ccomment 实体类
package com.entity;
import lombok.Data;
import java.util.Date;
import java.util.List;
/**
* @author 弃news
* @data 2021/12/11 20:47
*/
@Data
public class News {
private int nid;
private int ntid;
private String ntitle;
private String nauthor;
private Date ncreateDate;
private String npicPath;
private String ncontent;
private Date nmodifyDate;
private String nsummary;
private List<Comment> comments;
}
2.写实现对应功能的接口与xml
NewsMapper接口
package com.mapper;
import com.entity.News;
import java.util.List;
/**
* @author 弃
* @data 2021/12/11 16:22
*/
public interface NewsMapper {
List<News> AllNewss(int id);
}
NewsMapper.xml
namespace=对应接口的全路径
resultMap id=自定义命名 type=需要映射的实体类全路径
id column=对应数据库主键名 property=需要映射的实体类属性名
result column=数据库其他列的名字 property=对应的实体类属性
collection property=对应实体类属性的名字 ofType=对应集合类型全路径
id column=对应数据库主键名 property=需要映射的实体类属性名
result column=数据库其他列的名字 property=对应的实体类属性
select id= parameterType= resultMap=
<select id=接口方法名 parameterType=传入参数类型 resultMap id自定义的名字>
查询SQL
</select>
<?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.mapper.NewsMapper">
<resultMap id="NewsMap" type="com.entity.News">
<id column="id" property="id"></id>
<result column="ntid" property="ntid"></result>
<result column="ntitle" property="ntitle"></result>
<result column="nauthor" property="nauthor"></result>
<result column="ncreateDate" property="ncreateDate"></result>
<result column="npicPath" property="npicPath"></result>
<result column="ncontent" property="ncontent"></result>
<result column="nmodifyDate" property="nmodifyDate"></result>
<result column="nsummary" property="nsummary"></result>
<collection property="comments" ofType="com.entity.Comment">
<id column="cid" property="cid"></id>
<result column="cnid" property="cnid"></result>
<result column="ccontent" property="ccontent"></result>
<result column="cdate" property="cdate"></result>
<result column="cip" property="cip"></result>
<result column="cauthor" property="cauthor"></result>
</collection>
</resultMap>
<select id="AllNewss" parameterType="int" resultMap="NewsMap">
select
n.nid,
n.ntid,
n.ntitle,
n.nauthor,
n.ncreateDate,
n.npicPath,
n.ncontent,
n.nmodifyDate,
n.nsummary,
c.cid,
c.cnid,
c.ccontent,
c.cdate,
c.cip,
c.cauthor
from news n,
comments c
where
n.nid = #{id}
AND
c.cnid = n.nid
</select>
</mapper>
3.引入映射
config.xml
<mappers>
<mapper resource="接口相对应的xml全路径包括后缀"></mapper>
</mappers>
<?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="database.properties"></properties>
<settings>
<!-- 打印查询语句 -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!-- 配置mybatis运行环境 -->
<environments default="development">
<environment id="development">
<!-- 使用JDBC的事务管理 -->
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- MySQL数据库驱动 -->
<property name="driver" value="${driver}"/>
<!-- 连接数据库的URL -->
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- 引入映射配置文件 -->
<mappers>
<mapper resource="com/mapper/NewsMapper.xml"></mapper>
</mappers>
</configuration>
database.properties
配置要连接到的数据库以及用户名密码
driver=com.mysql.jdbc.Driver
#在和mysql传递数据的过程中,使用unicode编码格式,并且字符集设置为utf-8
url=jdbc:mysql://localhost:3306/one?useUnicode=true&characterEncoding=UTF-8
username=root
password=123456
用到的数据库SQL
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for news
-- ----------------------------
DROP TABLE IF EXISTS `news`;
CREATE TABLE `news` (
`nid` int(11) NOT NULL AUTO_INCREMENT,
`ntid` int(11) NOT NULL,
`ntitle` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`nauthor` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`ncreateDate` datetime DEFAULT NULL,
`npicPath` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`ncontent` text COLLATE utf8_unicode_ci NOT NULL,
`nmodifyDate` datetime DEFAULT NULL,
`nsummary` varchar(4000) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`nid`),
KEY `NEWS_TOPIC` (`ntid`),
CONSTRAINT `NEWS_TOPIC` FOREIGN KEY (`ntid`) REFERENCES `topic` (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=188 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- ----------------------------
-- Records of news
-- ----------------------------
INSERT INTO `news` VALUES ('108', '28', '惊奇!!小虫吃大蛇!', '环球搜奇 ', '2010-10-01 16:17:32', null, '小虫子把蛇吃了。。真猛!', '2010-10-01 16:17:32', '动物,大战,不可能,小虫,大蛇,未解之谜,自然,罕见');
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50527
Source Host : 127.0.0.1:3306
Source Database : newsmanagersystem
Target Server Type : MYSQL
Target Server Version : 50527
File Encoding : 65001
Date: 2021-11-22 08:46:38
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for comments
-- ----------------------------
DROP TABLE IF EXISTS `comments`;
CREATE TABLE `comments` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`cnid` int(11) NOT NULL,
`ccontent` varchar(3000) COLLATE utf8_unicode_ci NOT NULL,
`cdate` datetime NOT NULL,
`cip` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`cauthor` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`cid`),
KEY `CIN_NID` (`cnid`),
CONSTRAINT `CIN_NID` FOREIGN KEY (`cnid`) REFERENCES `news` (`nid`)
) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- ----------------------------
-- Records of comments
-- ----------------------------
INSERT INTO `comments` VALUES ('4', '73', '无聊!!', '2010-08-10 13:43:53', '127.0.0.1', '这家伙很懒什么也没留下');
INSERT INTO `comments` VALUES ('5', '108', '很饱啊', '2010-08-10 15:49:52', '127.0.0.1', '那只虫子');
INSERT INTO `comments` VALUES ('11', '82', '评论一下 我是admin', '2009-11-23 03:57:58', '127.0.0.1', 'admin');
INSERT INTO `comments` VALUES ('13', '81', '低碳生活', '2010-03-30 03:00:55', '10.0.0.35', '这家伙很懒什么也没留下');
INSERT INTO `comments` VALUES ('14', '86', '隔岸观火吃鸡腿~!', '2010-09-30 01:55:38', '127.0.0.1', 'admin');
INSERT INTO `comments` VALUES ('15', '73', '无聊!!', '2010-10-08 13:43:53', '127.0.0.1', '这家伙很懒什么也没留下');
INSERT INTO `comments` VALUES ('16', '108', '很饱啊', '2010-08-10 15:49:52', '127.0.0.1', '那只虫子');
INSERT INTO `comments` VALUES ('17', '108', '我郁闷啊!!', '2010-10-13 14:24:02', '127.0.0.1', '那只蛇');
INSERT INTO `comments` VALUES ('19', '108', '别回来了,都吃完了', '2010-10-13 14:35:42', '127.0.0.1', '妈妈');
INSERT INTO `comments` VALUES ('21', '108', '虫子,你妈妈叫你回家吃饭', '2010-10-13 14:26:58', '127.0.0.1', '打酱油的');
INSERT INTO `comments` VALUES ('23', '165', 'abc', '2013-05-30 15:11:35', '127.0.0.1', '这家伙很懒什么也没留下');
INSERT INTO `comments` VALUES ('32', '170', '好惨呀!', '2013-06-08 10:57:57', '127.0.0.1', '这家伙很懒什么也没留下');
INSERT INTO `comments` VALUES ('40', '170', '神马情况啊,这是??', '2013-10-28 16:32:44', '127.0.0.1', 'admin');
Test测试类
import com.entity.News;
import com.mapper.NewsMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.List;
/**
* @author 弃
* @data 2021/12/10 14:52
*/
public class Test {
public static void main(String[] args) {
//加载MyBatis配置⽂件
InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
List<News> news = newsMapper.AllNewss(108);
for (News news1 : news) {
System.out.println(news1.toString());
}
// sqlSession.commit();
sqlSession.close();
}
}
用到的依赖jar包maven配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mavenjava02</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!--<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-mxj</artifactId>
<version>5.0.12</version>
</dependency>-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<!-- 测试类 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<!-- 简化实体类的编写 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
<!--<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>-->
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>