1.首先到创建个数据库建个表吧
也许这样写无用,但是当个记录吧
CREATE TABLE `country` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`countryname` varchar(255) DEFAULT NULL,
`countrycode` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
CREATE TABLE `sys_privilege` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '权限ID',
`privilege_name` varchar(50) DEFAULT NULL COMMENT '权限名称',
`privilege_url` varchar(255) DEFAULT NULL COMMENT '权限url',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT='权限表';
CREATE TABLE `sys_role` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '角色ID',
`role_name` varchar(50) DEFAULT NULL COMMENT '角色名',
`enabled` bigint(11) DEFAULT NULL COMMENT '有效标志',
`create_by` bigint(20) DEFAULT NULL COMMENT '创建人',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COMMENT='角色表';
CREATE TABLE `sys_role_privilege` (
`role_id` bigint(20) DEFAULT NULL COMMENT '角色ID',
`privilege_id` bigint(20) DEFAULT NULL COMMENT '权限ID'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='角色权限关联表';
CREATE TABLE `sys_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`user_name` varchar(50) DEFAULT NULL COMMENT '用户名',
`user_password` varchar(50) DEFAULT NULL COMMENT '密码',
`user_email` varchar(50) DEFAULT NULL COMMENT '邮箱',
`user_info` text COMMENT '简介',
`head_img` blob COMMENT '头像',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
CREATE TABLE `sys_user_role` (
`user_id` bigint(20) DEFAULT NULL COMMENT '用户Id',
`role_id` bigint(20) DEFAULT NULL COMMENT '角色ID'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户角色关联表';
插入表中的数据
INSERT INTO `country` VALUES ('1', '中国', 'CN');
INSERT INTO `country` VALUES ('2', '美国', 'US');
INSERT INTO `country` VALUES ('3', '俄罗斯', 'RU');
INSERT INTO `country` VALUES ('4', '英国', 'GB');
INSERT INTO `country` VALUES ('5', '法国', 'FR');
INSERT INTO `sys_privilege` VALUES ('1', '用户管理', '/users');
INSERT INTO `sys_privilege` VALUES ('2', '角色管理', '/roles');
INSERT INTO `sys_privilege` VALUES ('3', '系统日志', '/logs');
INSERT INTO `sys_privilege` VALUES ('4', '人员维护', '/persons');
INSERT INTO `sys_privilege` VALUES ('5', '单位维护', '/companies');
INSERT INTO `sys_role` VALUES ('1', '管理员', '1', '1', '2018-05-10 09:17:33');
INSERT INTO `sys_role` VALUES ('2', '普通用户', '1', '1', '2018-05-10 09:17:50');
INSERT INTO `sys_role` VALUES ('5', 'xiaomi', '1', '1', '2018-05-10 20:26:52');
INSERT INTO `sys_role` VALUES ('6', 'xiaomi', '1', '1', '2018-05-10 20:31:05');
INSERT INTO `sys_role_privilege` VALUES ('1', '1');
INSERT INTO `sys_role_privilege` VALUES ('1', '3');
INSERT INTO `sys_role_privilege` VALUES ('1', '2');
INSERT INTO `sys_role_privilege` VALUES ('2', '4');
INSERT INTO `sys_role_privilege` VALUES ('2', '5');
INSERT INTO `sys_user` VALUES ('1', '管理员', '123456', 'admin@163.com', '管理员', null, '2018-05-10 09:16:25');
INSERT INTO `sys_user` VALUES ('2', 'test', '123456', '123456', '测试用户', null, '2018-05-10 09:17:04');
INSERT INTO `sys_user` VALUES ('9', 'aQ', '123456', 'ale@163.com', 'aQ', null, '2018-05-10 16:09:38');
INSERT INTO `sys_user` VALUES ('10', 'aQ', '123456', 'ale@163.com', 'aQ', null, '2018-05-10 16:18:18');
INSERT INTO `sys_user` VALUES ('11', 'aQ', '123456', 'ale@163.com', 'aQ', null, '2018-05-10 16:18:44');
INSERT INTO `sys_user` VALUES ('12', 'aQ', '123456', 'ale@163.com', 'aQ', null, '2018-05-11 14:29:42');
INSERT INTO `sys_user_role` VALUES ('1', '1');
INSERT INTO `sys_user_role` VALUES ('1', '2');
INSERT INTO `sys_user_role` VALUES ('2', '2');
3.创建实体类 和Mapper.xml(只提供属性)
public class Country {
private Long id;
private String countryname;
private String countrycode;
}
创建Mapper接口CountryMapper.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 命名空间 此处需要和你的CountryMapper 接口在同一个目录下 并且名字一样 -->
<mapper namespace="com.shouyiren.mapper.CountryMapper">
<!-- SQL语句 -->
<!-- resultType 需要返回的实体类的别名 -->
<select id="selectAll" resultType="Country">
select id,countryname,countrycode from country
</select>
</mapper>
创建接口CountryMapper.java
public interface CountryMapper {
public List<Country> selectAll();
}
4. 添加mybatis-config的配置文件
<?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>
<!-- -->
<settings>
<!-- 添加的log日志 -->
<setting name="logImpl" value="LOG4J"/>
<!-- 自动映射驼峰命名法 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- 给实体类添加别名
只需要填写 实体类所在的包
-->
<typeAliases>
<package name="com.shouyiren.entry"/>
</typeAliases>
<!-- 数据库的连接信息 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&socketTimeout=60000"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 引入 Mapper的配置文件 -->
<mappers>
<!-- <package name="com/shouyiren/mapper"/> -->
<mapper resource="com/shouyiren/mapper/CountryMapper.xml"/>
</mappers>
</configuration>
5.添加log4j.properties日志配置信息
### 设置###
log4j.rootLogger = ERROR, stdout
#MyBatis 日志配置
#此处需要修改为自己Mapper的包名
log4j.logger.com.shouyiren.mapper=TRACE
### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =%5p [%t] - %m%n
6. 此处使用的Maven需要添加jar包坐标
<!-- 使用编码集为utf8 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 添加Mybatis的依赖 -->
<dependencies>
<!-- 单元测试 begin -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 单元测试 end -->
<!-- mybatis begin -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<!-- mybatis end -->
<!-- mysql begin -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- mysql end -->
<!-- 日志 begin -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- 日志 end -->
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<source>1.8</source>
</configuration>
</plugin>
</plugins>
</build>
7.书写测试类
package com.shouyiren.test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
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 org.junit.BeforeClass;
import org.junit.Test;
import com.shouyiren.entry.Country;
public class CountryMapperTest {
//获取sqlSessionFactory 工厂根据 工厂创建SQLSession 对象连接
private static SqlSessionFactory sqlSessionFactroy;
//此处读取mybatis-config的配置文件信息
@BeforeClass
public static void init(){
try {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactroy = new SqlSessionFactoryBuilder().build(reader);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testSelectAll(){
//获取sqlSession
SqlSession openSession = sqlSessionFactroy.openSession();
//此处使用的根据CountryMapper 接口中的方法名 查询数据 (原理以后说)
List<Country> list = openSession.selectList("selectAll");
printCountryList(list);
openSession.close();
}
private void printCountryList(List<Country> list) {
for (Country country : list) {
System.out.printf("%-4d%4s%4s\n",country.getId(),country.getCountryname(),country.getCountrycode());
}
}
}
8. 测试结果和项目视图
https://img-blog.csdn.net/20180511230528299
https://img-blog.csdn.net/20180511230528597