mybatis
Posted 大赵传奇
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis相关的知识,希望对你有一定的参考价值。
1.新建个maven,注意一开始只勾一个,不选下面的starter什么的,进来就有maven的目录结构
-- Create table
create table USERS
(
id NUMBER(10),
username VARCHAR2(20),
age NUMBER(3)
)
pom.xml
<?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>test5</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>com.oracle.ojdbc</groupId> <artifactId>ojdbc8</artifactId> <scope>runtime</scope> <version>19.3.0.0</version> </dependency> <!-- 去除异常 java.sql.SQLException: 不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK--> <dependency> <groupId>cn.easyproject</groupId> <artifactId>orai18n</artifactId> <version>12.1.0.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> <version>2.2.2.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <defaultGoal>compile</defaultGoal> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.2.2.RELEASE</version> </plugin> </plugins> </build> </project>
IUserDao.java
package dao; import domain.User; public interface IUserDao { User GetUserByID(Integer id) throws Exception; }
User.java
package domain; import org.apache.ibatis.type.Alias; @Alias(value="user1") //测试指定别名 public class User { private int id; private String user_Name; //这里测试别名 private int age; //为了测试,console.writeln能显示值 toString方法 //getter seter方法也可以不要 @Override public String toString() { return "User{" + "id=" + id + ", name=\'" + user_Name + \'\\\'\' + ", dept=\'" + age + \'\\\'\' + \'}\'; } }
userMap.xml
<?xml version="1.0" encoding="UTF-8" ?> <!-- mapper 约束--> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--namespace 写的是 dao权限类名--> <mapper namespace="dao.IUserDao" > <!--查询用户--> <select id="GetUserByID" resultType="domain.User"> select id, username as user_Name, age from users where id=#{id} </select> </mapper>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!-- configuration 约束--> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!--mybatis的主配置文件--> <configuration> <!-- 配置环境 --> <environments default="oracle"> <!--配置mysql环境--> <environment id="oracle"> <!-- 配置事务类型 --> <transactionManager type="JDBC"/> <!-- 配置数据源(连接池) --> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"/> <property name="username" value="test1"/> <property name="password" value="123"/> </dataSource> </environment> </environments> <!--指定映射配置文件的位置,映射配置文件指的是每个dao配置的文件--> <mappers> <!--使用的是xml--> <mapper resource="mapper/userMap.xml"/> <!--<mapper class="com..dao.IUserMapper"/> ;使用的是注解 --> </mappers> </configuration>
MyBatisTest.java
import dao.IUserDao; import domain.User; 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.After; import org.junit.Before; import org.junit.Test; import java.io.InputStream; import java.util.List; public class MybatisTest { InputStream in; SqlSession sqlSession; IUserDao userDao; @Before//初始化 public void init()throws Exception{ //1.读取配置文件 in = Resources.getResourceAsStream("mybatis/mybatis-config.xml"); //2.创建SqlSessionFactory工厂 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(in); //3.使用工厂创建SqlSession sqlSession =factory.openSession(); //4.使用SqlSession创建dao接口的代理对象 userDao =sqlSession.getMapper(IUserDao.class); } @After//测试结束之后执行 public void destory() throws Exception{ //6.释放资源 sqlSession.close(); in.close(); } @Test//查询 public void test_getUserById() throws Exception { //5.使用代理对象的方法 User users = userDao.GetUserByID(5); System.out.println(users); } }
以上是关于mybatis的主要内容,如果未能解决你的问题,请参考以下文章
SSM-MyBatis-05:Mybatis中别名,sql片段和模糊查询加getMapper
MYBATIS05_ifwherechoosewhentrimsetforEach标签sql片段