MyBatis笔记1 开始使用
来源:互联网 发布:cctv网络电视 编辑:程序博客网 时间:2024/06/14 08:58
本节写个简单的Spring + MyBatis + MySQL应用
- Step 1. 新建一个目录:D:\Workspaces\MyStudyRepository\StudyMybatis
- Step 2. 运行:gradle init --type java-library
- Step 3. 在Eclipse中import gradle project, 选择这个目录
- Step 4. 在build.gradle中,加入需要的依赖
compile group: 'org.mybatis', name: 'mybatis', version: '3.4.2'
compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.3.1'
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.5'
- Refresh Gradle Project, 下载jar包
- Step 5. 在Mysql中创建两个表做例子
CREATE TABLE `t_product_category` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `t_product` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
`product_category_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`Id`),
KEY `FK_PRODUCT_CATEGORY_ID` (`product_category_id`),
CONSTRAINT `FK_PRODUCT_CATEGORY_ID` FOREIGN KEY (`product_category_id`) REFERENCES `t_product_category` (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- Step 6. 在Eclipse中,查找并安装插件:MyBatis Generator, 当前版本:1.3.5 (http://www.mybatis.org/generator/)
- Step 7. File/New/Others/MyBatis Generator Configuration File
- 建立在StudyMybatis根目录下"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:\gradle_repo\caches\modules-2\files-2.1\Mysql\mysql-connector-java\6.0.5\9d8df5a1f3273110f2a9ad304961bdd03d696d86\mysql-connector-java-6.0.5.jar"/>
<context id="context1">
<jdbcConnection
connectionURL="jdbc:mysql://***"
driverClass="com.mysql.cj.jdbc.Driver"
password="***"
userId="***" />
<javaModelGenerator targetPackage="com.cloudboy.mybatis.model" targetProject="StudyMybatis\src\main\java" />
<sqlMapGenerator targetPackage="mybatis" targetProject="StudyMybatis\src\main\resources" />
<javaClientGenerator targetPackage="com.cloudboy.mybatis.mapper" targetProject="StudyMybatis\src\main\java" type="XMLMAPPER" />
<table schema="feitu" tableName="t_product_category">
</table>
<table schema="feitu" tableName="t_product">
</table>
</context>
</generatorConfiguration> - 文件里配置了数据库连接URL及用户名密码,此工具需要读取数据库表结构,以便自动生成代码和配置xml
- javaModelGenerator: 配置java数据模型存放的位置
- sqlMapGenerator: 配置xml配置文件存放的位置
- javaClientGenerator: 配置java DAO接口文件的位置
- 配置了我们新建的两个表t_product_category, t_product
- Step 8. 右键点击这个文件,选择Run As/Run MyBatis Generator, 以下文件自动生成出来:
- src/main/java/com.cloudboy.mybatis.model包下
- TProduct.java 数据模型类,对应表T_PRODUCT
- TProductCategory.java 数据模型类,对应表T_PRODUCT_CATEGORY
- TProductExample.java 用来生成表T_PRODUCT查询条件的辅助类
- TProductCategoryExample.java 用来生成表T_PRODUCT_CATEGORY查询条件的辅助类
- src/main/java/com.cloudboy.mybatis.mapper包下
- TProductMapper.java // 访问表T_PRODUCT的接口,相当于DAO
- TProductCategoryMapper.java // 访问表T_PRODUCT_CATEGORY的接口,相当于DAO
- TProductMapper.java // 访问表T_PRODUCT的接口,相当于DAO
- src/main/resources/mybatis下
- TProductMapper.xml // 表的mapper配置
- TProductCategoryMapper.xml // 表的mapper配置
- TProductMapper.xml // 表的mapper配置
- src/main/java/com.cloudboy.mybatis.model包下
- Step 9. 新建 src/main/resources/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>
<mappers>
<mapper resource="mybatis/TProductMapper.xml"/>
<mapper resource="mybatis/TProductCategoryMapper.xml"/>
</mappers>
</configuration> - 里面包含了2个表的Mapper
- Step 10. 新建 src/main/resources/myBatisBean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.cj.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://mysql.rdsm5qi8p7obva1.rds.gz.baidubce.com/feitu?useUnicode=true&characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>sysfeitu</value>
</property>
<property name="password">
<value>xy65390856</value>
</property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
</bean>
<bean id="productMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.cloudboy.mybatis.mapper.TProductMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="productCategoryMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.cloudboy.mybatis.mapper.TProductCategoryMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
- 定义了数据库连接方式
- 定义了两个Mapper bean
- Step 11. 写一个总的spring配置文件:/src/main/resources/applicationContext.xml, 里面import myBatisBean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<aop:aspectj-autoproxy />
<import resource="classpath:mybatis/myBatisBean.xml"/>
</beans> - Step 12. 写测试类
package com.cloudboy.mybatis;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cloudboy.mybatis.mapper.TProductCategoryMapper;
import com.cloudboy.mybatis.model.TProductCategory;
import com.cloudboy.mybatis.model.TProductCategoryExample;
import com.thoughtworks.xstream.XStream;
public class ProductCategoryTest {
private static ApplicationContext ctx;
private static TProductCategoryMapper dao = null;
private static XStream xs = new XStream();
@BeforeClass
public static void setUpBeforeClass() throws Exception {
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
dao = (TProductCategoryMapper)ctx.getBean("productCategoryMapper");
}
@Test
public void testInsert() {
for(int i=0; i<10; i++) {
TProductCategory data = new TProductCategory();
data.setName("商品" + i);
dao.insert(data);
}
}
@Test
public void testSelect() {
TProductCategoryExample example = new TProductCategoryExample();
// 啥条件也不写,则返回全部记录
List<TProductCategory> result = dao.selectByExample(example);
System.out.println(xs.toXML(result));
// 选择id在3~5的记录
example.createCriteria().andIdBetween(3, 5);
result = dao.selectByExample(example);
System.out.println(xs.toXML(result));
}
@Test
public void testUpdate() {
TProductCategory data = new TProductCategory();
data.setId(4);
data.setName("旅游4");
TProductCategoryExample example = new TProductCategoryExample();
example.createCriteria().andIdEqualTo(4);
dao.updateByExample(data, example);
}
}
本例的依赖
dependencies {
testImplementation 'junit:junit:4.12'
testImplementation group:'com.thoughtworks.xstream', name:'xstream', version:'1.4.7'
testImplementation group:'xmlpull', name:'xmlpull', version:'1.1.3.4d_b4_min'
compile group: 'org.mybatis', name: 'mybatis', version: '3.4.2'
compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.3.1'
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.5'
compile group: 'org.springframework', name: 'spring-context', version: '4.3.8.RELEASE'
compile group: 'org.springframework', name: 'spring-jdbc', version: '4.3.8.RELEASE'
compile group: 'org.aspectj', name: 'aspectjrt', version: '1.8.10'
}
阅读全文
0 0
- MyBatis笔记1 开始使用
- Mybatis学习笔记(一)--- 开始使用Mybatis
- 开始使用MyBatis
- 开始使用mybatis
- Mybatis使用笔记
- mybatis开始第1天
- Mybatis学习笔记-MyBatis缓存的使用
- QQZiFramework笔记:开始使用DBContext
- mybatis的简单使用笔记
- openlayers使用笔记1 从例子开始学习openlayers
- 转载 openlayers使用笔记1 从例子开始学习openlayers
- Python笔记1:开始
- ANSYS使用笔记(一):开始使用
- Mybatis学习笔记(1)
- mybatis笔记1
- mybatis笔记《1》
- MyBatis学习笔记(1)
- mybatis笔记1
- 解决 Button 和 TabLayout 英文自动大写的问题
- http://blog.csdn.net/lmj623565791/article/details/38339817
- ngx_lua 模块
- Eclipse Neon中找不到Marketplace的解决办法
- 数据如何成为企业未来的商业资产?
- MyBatis笔记1 开始使用
- oracle表空间、用户创建、授权sql
- matlab字体忽然变小解决办法
- 关于james邮箱服务器配置、维护以及常见问题定位
- Java Web应用中调优线程池的重要性
- org.springframework.web.util.IntrospectorCleanupListener
- idea不正常关闭,导致项目异常的解决办法
- a short introduction to scala
- WebService接口服务器(XFire)