Java框架-mybatis01查询单个数据
1.什么是mybatis?
mybatis是一个基于Java的持久层框架。
2.持久化:数据从瞬时状态变为持久状态。
3.持久层:完成持久化工作的代码块。---dao
4.Mybatis是帮助程序员将数据存入数据库中,从数据库中取数据。
5.传统的jdbc操作:有很多重复代码块。比如:数据取出时的封装。数据库的建立连接等。通过框架可以减少重复代码,提高开发效率。
6.Mybatis是一个半自动化的ORM框架。O---object,R---relationship,M---mapping
7.Mybatis的功能:
MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。Mybatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。
8.如何使用mybatis?
1)导入mybatis相关jar包
asm-3.3.1.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
javassist-3.17.1-GA.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
数据库驱动包
如果使用maven构建项目,需要在pom.xml文件中插入以下代码:
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> </dependencies>
2)编写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> <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://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="12345678"/> </dataSource> </environment> </environments> <mappers> <mapper resource="userMapper.xml"/> </mappers> </configuration>
3)创建SqlSessionFactory以及从 SqlSessionFactory 中获取 SqlSession
public class MyBatisUtil { public static SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); return sqlSessionFactory; } public static SqlSession getSession() throws IOException { SqlSessionFactory sqlSessionFactory=getSqlSessionFactory(); return sqlSessionFactory.openSession(); } }
4)创建实体类,定义属性与数据库一致
public class User { private int id; private String name; private String age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
5)编写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"> <mapper namespace="userMapper"> <select id="selectUser" resultType="cn.sxt.entity.User"> select * from student where id = #{id} </select> </mapper>
6)进行测试
public class Test { public static void main(String[] args) throws IOException { SqlSession session= MyBatisUtil.getSession(); User user=session.selectOne("userMapper.selectUser",1002); System.out.println("id="+user.getId()+";"+"name="+user.getName()+";"+"age="+user.getAge()); session.close(); } }
表结构
idea结构
mybatis帮助文档:http://www.mybatis.org/mybatis-3/zh/configuration.html#typeAliases