赞
踩
这操作真心记不住。。。。。还是写一遍加深记忆
1.首先创建user表,添加数据.
- create datebase fuck;
-
- use fuck;
-
- // 删除tb_brand表
- drop table if exists tb_brand;
- // 创建tb_brand表
- create table tb_brand
- (
- -- id 主键
- id int primary key auto_increment,
- -- 品牌名称
- brand_name varchar(20),
- -- 企业名称
- company_name varchar(20),
- -- 排序字段
- ordered int,
- -- 描述信息
- description varchar(100),
- -- 状态:0:禁用 1:启用
- status int
- );
- // 添加数据
- insert into tb_brand (brand_name, company_name, ordered, description, status)
- values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0),
- ('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', 1),
- ('小米', '小米科技有限公司', 50, 'are you ok', 1);
2.创建Maven模块,导入坐标
导入坐标
- <dependencies>
- <!--mysql驱动-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.28</version>
- </dependency>
- <!--mybatis的依赖-->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.5.5</version>
- </dependency>
- <!--junit单元测试-->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- <scope>test</scope>
- </dependency>
-
- <!-- 添加slf4j日志api -->
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.7.20</version>
- </dependency>
- <!-- 添加logback-classic依赖 -->
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-classic</artifactId>
- <version>1.2.3</version>
- </dependency>
- <!-- 添加logback-core依赖 -->
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-core</artifactId>
- <version>1.2.3</version>
- </dependency>
-
- </dependencies>
3.编写MyBatis的核心配置文件,并且导入logback.xml文件
(1)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>
-
- <!--起别名-->
- <typeAliases>
- <package name="com.fuck.pojo"/>
- </typeAliases>
-
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC"/>
- <dataSource type="POOLED">
- <!--数据库连接信息-->
- <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql:///fuck?useSSL=false&useServerPrepStmts=true"/>
- <property name="username" value="root"/>
- <property name="password" value="123456"/>
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <!--扫描mapper-->
- <package name="com.fuck.mapper"/>
- </mappers>
- </configuration>
(2) logback.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <configuration>
- <!--
- CONSOLE :表示当前的日志信息是可以输出到控制台的。
- -->
- <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
- <encoder>
- <pattern>[%level] %blue(%d{HH:mm:ss.SSS}) %cyan([%thread]) %boldGreen(%logger{15}) - %msg %n</pattern>
- </encoder>
- </appender>
-
- <logger name="com.fuck" level="DEBUG" additivity="false">
- <appender-ref ref="Console"/>
- </logger>
-
-
- <!--
- level:用来设置打印级别,大小写无关:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF
- , 默认debug
- <root>可以包含零个或多个<appender-ref>元素,标识这个输出位置将会被本日志级别控制。
- -->
- <root level="DEBUG">
- <appender-ref ref="Console"/>
- </root>
- </configuration>
4.编写pojo实体类,用get 、set方法
- package com.fuck.pojo;
-
- public class Brand {
- private Integer id;
- private String brandName;
- private String companyName;
- private Integer ordered;
- private String description;
- private Integer status;
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getBrandName() {
- return brandName;
- }
-
- public void setBrandName(String brandName) {
- this.brandName = brandName;
- }
-
- public String getCompanyName() {
- return companyName;
- }
-
- public void setCompanyName(String companyName) {
- this.companyName = companyName;
- }
-
- public Integer getOrdered() {
- return ordered;
- }
-
- public void setOrdered(Integer ordered) {
- this.ordered = ordered;
- }
-
- public String getDescription() {
- return description;
- }
-
- public void setDescription(String description) {
- this.description = description;
- }
-
- public Integer getStatus() {
- return status;
- }
-
- public void setStatus(Integer status) {
- this.status = status;
- }
-
- @Override
- public String toString() {
- return "Brand{" +
- "id=" + id +
- ", brandName='" + brandName + '\'' +
- ", companyName='" + companyName + '\'' +
- ", ordered=" + ordered +
- ", description='" + description + '\'' +
- ", status=" + status +
- '}';
- }
- }
5.创建mapper接口
- package com.fuck.mapper;
- import com.fuck.pojo.Brand;
- import java.util.List;
- public interface BrandMapper {
- //查询所有
- public List<Brand> selectAll();
- }
6.创建SqlSessionFactory工具类
- package com.fuck.Utils;
-
- import org.apache.ibatis.io.Resources;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
- import java.io.IOException;
- import java.io.InputStream;
- public class SqLSessionFactoryUtils {
- private static SqlSessionFactory sqlSessionFactory;
- static {
- String resource = "mybatis-config.xml";
- InputStream inputStream = null;
- try {
- inputStream = Resources.getResourceAsStream(resource);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- }
- public static SqlSessionFactory getSqlSessionFactory(){
- return sqlSessionFactory;
- }
- }
总的项目结构
7.创建Mapper映射文件,并写入sql查询语句
BrandMapper.xml文件
- <?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">
-
- <!--namespace名称空间-->
- <mapper namespace="com.fuck.mapper.BrandMapper">
- <!--resultMap:
- 1.定义resultMap标签
- 2.在<select>标签中,使用resultMap属性替换resultType属性
- -->
- <resultMap id="brandResultMap" type="brand">
- <!-- id:完成主键字段映射-->
- <result column="brand_name" property="brandName"></result>
- <result column="company_name" property="companyName"></result>
- </resultMap>
- <!--查询所有-->
- <select id="selectAll" resultMap="brandResultMap">
- select * from tb_brand;
- </select>
- </mapper>
8. 最后编写测试类
MyBatisTest
- package com.fuck.test;
-
- import com.fuck.Utils.SqLSessionFactoryUtils;
- import com.fuck.mapper.BrandMapper;
- import com.fuck.pojo.Brand;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.junit.Test;
-
- import java.util.Base64;
- import java.util.List;
-
- public class MyBatisTest {
- // 调用SqlSessionFactory方法 构建出SqlSessionFactory的实例 factory
- SqlSessionFactory factory= SqLSessionFactoryUtils.getSqlSessionFactory();
- // 利用注解
- @Test
- public void testSelectAll(){
- // 获取SqlSession
- SqlSession sqlSession= factory.openSession();
- // 获取BrandMapper
- BrandMapper brandmapper=sqlSession.getMapper(BrandMapper.class);
- // 调用方法
- List<Brand> brands=brandmapper.selectAll();
- // 输出
- System.out.println(brands);
- // 关闭sqlSession
- sqlSession.close();
-
- }
- }
运行结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。