Maven项目导入依赖
<?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>cn.ronghuanet</groupId>
<artifactId>mybatis-day02</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.1</version>
</dependency>
<!-- mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<!-- junit测试包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<!-- 局部jdk 1.8配置,pom.xml中 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Mybatis集成
Mybatis配置文件
logo4j.properties
#\u5168\u5C40\u65E5\u5FD7\u914D\u7F6E
log4j.rootLogger = info,console
#log level:
#debug is used by develop, error or warn is used by online
log4j.logger.com.ronghuanet=debug
### \u914D\u7F6E\u8F93\u51FA\u5230\u63A7\u5236\u53F0
log4j.appender.console = org.apache.log4j.ConsoleAppender
### \u4F7F\u7528System.out\u6253\u5370\u65E5\u5FD7
log4j.appender.console.Target = System.out
### \u6307\u5B9A\u65E5\u5FD7\u7684\u683C\u5F0F\u5E03\u5C40(\u65E5\u5FD7\u662F\u6709\u683C\u5F0F\u7684)
log4j.appender.console.layout = org.apache.log4j.PatternLayout
### \u65E5\u5FD7\u7684\u6253\u5370\u683C\u5F0F
log4j.appender.console.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n
jdbcproperties
jdbc.username=root
jdbc.password=admin
jdbc.url=jdbc:mysql:///mybatis
jdbc.driverClassName=com.mysql.jdbc.Driver
mybatis-config.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>
<!-- 加载jdbc.properties-->
<properties resource="jdbc.properties"/>
<!--申明操作数据库的环境-->
<environments default="MYSQL">
<environment id="MYSQL">
<!--使用jdbc的事务-->
<transactionManager type="JDBC"/>
<!--支持连接池-->
<dataSource type="POOLED">
<!--自动补全结构:ctrl+shift+回车-->
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driver" value="${jdbc.driverClassName}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 加载mapper.xml文件-->
</mappers>
</configuration>
创建Mybatis工具类并测试
public class MybatisUtils {
private static SqlSessionFactory sessionFactory ;
static{
try {
//提示处理异常快捷键 alt+回车 移动代码的快捷键 alt+上键或者下键
//快速使用变量接收, alt+回车
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
//根据io流创建SqlSessionFactory对象
sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession openSession(){
if (sessionFactory != null) {
return sessionFactory.openSession();
}
return null;
}
}
测试代码
public class MybatisTest {
@Test
public void test() throws Exception{
SqlSession sqlSession = MybatisUtils.openSession();
System.out.println(sqlSession);
MybatisUtils.closeSession(sqlSession);
}
}
创建模型
public class Product {
private Long id;
private String name;
private BigDecimal price;
/**
* alt+insert:快速生成setter getter方法 toString方法 构造方法
*/
//getter,setter代码略...
}
productMapper接口
public interface ProductMapper {
Product findOne(Long id);
}
创建对应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的内容就是ProductMapper接口的全限定名-->
<mapper namespace="org.test..mybatis._01_batch.mapper.ProductMapper">
<!--id的值保证ProductMapper接口的方法名一值-->
<select id="findOne" parameterType="long" resultType="org.test.mybatis._01_batch.domain.Product">
select * from product where id = #{id}
</select>
</mapper>
注册mapper.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>
...
<mappers>
<!-- 加载mapper.xml文件-->
<mapper resource="org/test/mybatis/_01_batch/mapper/ProductMapper.xml" />
</mappers>
</configuration>
获取单个对象
public class MyBatisTest {
/**
* 定义一个映射器mapper接口,使用mybatis自动为我们创建代理类
* @throws Exception
*/
@Test
public void findOne()throws Exception{
//获取到会话对象
SqlSession session = MybatisUtils.openSession();
//拿到映射对象,可以做相应的操作
ProductMapper mapper = session.getMapper(ProductMapper.class);
Product product = mapper.findOne(1L);
System.out.println(product);
}
}
本篇主要写了IDEA如何集成Mybatis,后续还会介绍mybatis映射等知识。笔者小,中,大厂均有面试经历,坚持每日分享java全栈知识,希望和大家共同进步。
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » JAVAWEB--Mybatis02
发表评论 取消回复