mybatis使用

本文介绍了如何利用Maven管理项目依赖,添加MyBatis并配置mybatis-generator插件自动生成ORM代码。通过配置文件实现数据源连接和表映射,最终在MyBatis配置文件中整合数据库连接和映射规则,完成从数据库到Java对象的映射。此外,展示了一个简单的MyBatis测试代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、使用Maven管理项目依赖,添加mybatis依赖,并配置mybatis generator插件自动生成mybatis代码

<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>com.qerooy</groupId>
<artifactId>mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mybatis</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>

<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
</plugin>
</plugins>
</build>
</project>




2、添加配置文件src/main/resource/generatorConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
<classPathEntry location="D:\.m2\repository\mysql\mysql-connector-java\5.1.29\mysql-connector-java-5.1.29.jar" />

<context id="MYsql2Tables" targetRuntime="MyBatis3">

<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin">
</plugin>

<commentGenerator>
<property name="suppressAllComments" value="false" />
<property name="suppressDate" value="true" />
</commentGenerator>

<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/test" userId="root" password="123jkl">
</jdbcConnection>

<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>

<javaModelGenerator targetPackage="com.qerooy.dto" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="true" />
</javaModelGenerator>

<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources/">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>

<javaClientGenerator type="XMLMAPPER" targetPackage="com.qerooy.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>

<table schema="root" tableName="user" domainObjectName="User" />

</context>
</generatorConfiguration>


3、myeclipse中执行mvn命令 -Dmybatis.generator.overwrite=true mybatis-generator:generate,根据配置文件生成mybatis代码,项目结构如下所示
[img]http://dl2.iteye.com/upload/attachment/0098/1689/06747a4c-7ae6-34e0-83a7-ba3be3c17f98.png[/img]

4、接下来编写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>
<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://localhost/test" />
<property name="username" value="root" />
<property name="password" value="123jkl" />
</dataSource>
</environment>
</environments>

<mappers>
<mapper resource="mapper/UserMapper.xml" />
</mappers>
</configuration>



5、编写测试代码

package com.qerooy.mybatis;

import java.io.Reader;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.qerooy.dto.User;
import com.qerooy.dto.UserExample;
import com.qerooy.mapper.UserMapper;

public class MybatisTest {

@Test
public void testFind() throws Exception{
Reader reader = Resources.getResourceAsReader("config.xml");
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader );

SqlSession session = sqlMapper.openSession();
UserMapper userMapper = session.getMapper(UserMapper.class);

UserExample example = new UserExample();
RowBounds rowBounds = new RowBounds(0,2);
List<User> list = userMapper.selectByExampleWithRowbounds(example, rowBounds);

User record = new User();
record.setCreateDate(new Date());
record.setPassword("123");
record.setRemark("remark");
record.setUsername("qerooy");
userMapper.insert(record );

session.commit();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值