- 在pom.xml导入依赖
<dependencies> <!--导入mysql 驱动jar包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.32</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> <scope>test</scope> </dependency> <!--添加slf4j日志api--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.20</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> <!--添加logback-classic依赖--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.9</version> </dependency> <!--添加logback-core依赖--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.9</version> </dependency> </dependencies>
在userMapper.xml导入sql语句,进行sql语句的调用
<?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="test"> <select id="selectAll" resultType="com.Ye.User"> select *from tb_user; </select> </mapper>
在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.Ye" 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>
在mybatis-config.xml连接数据库信息。加载映射文件(写sql文件的.xml文件)。
<?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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <!--数据库的链接信息,链接数据库--> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///mybatis?useSSL=false"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <!--加载sql映射文件--> <mapper resource="UserMapper.xml"/> </mappers> </configuration>
在User.java导入关键字信息,用于MyBatisDemo.java文件的数组调用。
package com.Ye; public class User { private Integer id; private String username; private String password; private String gender; private String addr; public User() { } public User(Integer id, String username, String password, String gender, String addr) { this.id = id; this.username = username; this.password = password; this.gender = gender; this.addr = addr; } /** * 获取 * @return id */ public Integer getId() { return id; } /** * 设置 * @param id */ public void setId(Integer id) { this.id = id; } /** * 获取 * @return username */ public String getUsername() { return username; } /** * 设置 * @param username */ public void setUsername(String username) { this.username = username; } /** * 获取 * @return password */ public String getPassword() { return password; } /** * 设置 * @param password */ public void setPassword(String password) { this.password = password; } /** * 获取 * @return gender */ public String getGender() { return gender; } /** * 设置 * @param gender */ public void setGender(String gender) { this.gender = gender; } /** * 获取 * @return addr */ public String getAddr() { return addr; } /** * 设置 * @param addr */ public void setAddr(String addr) { this.addr = addr; } public String toString() { return "User{id = " + id + ", username = " + username + ", password = " + password + ", gender = " + gender + ", addr = " + addr + "}"; } }
在MyBatisDemo.java文件中
- 加载核心配置文件
- 获取Sqlsession对象,用它来执行sql语句
- 执行sql
- 释放资源
package com.Ye; 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; import java.util.List; public class MyBatisDemo { public static void main(String[] args) throws IOException { //1、加载MyBatis核心配置文件 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //2、获取SqlSession对象,用它执行sql SqlSession sqlSession = sqlSessionFactory.openSession(); //3、执行sql List<User> users = sqlSession.selectList("test.selectAll");//查询所有, 传入sql语句的唯一标识 //test(id)是唯一标识,selectAll(namespace)名称空间 //sqlSession.selectOne(); 查询一个 System.out.println(users);//打印users,显示结果 //4、释放资源 sqlSession.close(); } }
标签:username,String,gender,public,学习,MyBatis,日志,password,id From: https://www.cnblogs.com/yzx-sir/p/17244594.html