mybatis
SSM
-------------------------------
java web 框架
spring //业务层框架
spring MVC //开发web程序应用的模块model+ view +controller模块化编程。模型视图控制器展现部分放到一个模块中去,控制的部分放到一个文件中去,数据部分放到一个数据模块中去。当改变数据的时候前面的状态会有变化。
Mybatis //数据持久化技术。jcbc,mysql
一、mybatis
-------------------------------------------
前身ibatis.对jdbc的封装,
1.体验ibatis
----------------------------------
1.创建项目和模块
2.添加pom文件
<?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>com.it18zhang</groupId> <artifactId>idea-bigdata-project4</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.17</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
3.XML 配置文件(configuration XML)中包含了对 MyBatis 系统的核心设置,包含获取数据库连接实例的数据源(DataSource)和决定事务作用域和控制方式的事务管理器(TransactionManager)。XML 配置文件的详细内容后面再探讨,这里先给出一个简单的示例:
打开mysql:创建数据库:create database mybatis ; // create table users(id int primary_key,name varchar(20),age int)//创建表
[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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers> </configuration>
4.创建库和表
5.API编程测试连接
package com.it18zhang.mybatisdemo; 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 java.io.InputStream; /** * Created by stone on 2018/9/19. */ public class App { public static void main(String[] args){ try{ //指定配置文件的路径(类路径) String resource = "mybatis-config.xml"; //加载文件 InputStream inputStream = Resources.getResourceAsStream(resource); //创建回话工厂Builder,相当于连接池 SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream); //通过sf开启会话,相当于打开连接 SqlSession s=sf.openSession(); System.out.println(s); } catch(Exception e ){ e.printStackTrace(); } } }
6.编写映射文件
a.创建User类,和User表对应
package com.it18zhang.mybatisdemo.domain; /** * Created by stone on 2018/9/19. */ public class User { private Integer id ; private String name ; private int age ; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
b.创建UserMapper.xml文件
[com/it18zhang/mybatisdemo/domain/userMapper.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"> <mapper namespace="users "> <insert id="insert" > insert into users(name,age) values(#{NAME},#{age}) </insert> </mapper>
c.通过映射来进行数据库的CRUD
package com.it18zhang.mybatisDemo; import com.it18zhang.mybatisdemo.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.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; /** * Created by stone on 2018/9/19. */ public class testCRUD { /* * * insert * */ @Test public void insert(){ try { //添加配置文件(加载类路径) String resources = "mybatis-config.xml"; //加载文件 InputStream inputStream = Resources.getResourceAsStream(resources); //创建会话工厂Builder,相当于连接池 SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream); //通过sf开启会话,相当于打开连接 SqlSession s = sf.openSession(); User u = new User(); u.setId(2); u.setName("jerry"); u.setAge(13); s.insert("users.insert",u); s.commit(); s.close(); } catch (IOException e) { e.printStackTrace(); } } /* * uodate * */ @Test public void update(){ try { //添加配置文件(加载类路径) String resources = "mybatis-config.xml"; //加载文件 InputStream inputStream = Resources.getResourceAsStream(resources); //创建会话工厂Builder,相当于连接池 SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream); //通过sf开启会话,相当于打开连接 SqlSession s = sf.openSession(); User u = new User(); u.setId(2); u.setName("stone"); u.setAge(23); s.update("users.update",u); s.commit(); s.close(); } catch (IOException e) { e.printStackTrace(); } } /* * SelectOne * */ @Test public void selectOne(){ try { //添加配置文件(加载类路径) String resources = "mybatis-config.xml"; //加载文件 InputStream inputStream = Resources.getResourceAsStream(resources); //创建会话工厂Builder,相当于连接池 SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream); //通过sf开启会话,相当于打开连接 SqlSession s = sf.openSession(); User u = s.selectOne("users.selectOne",1); System.out.println(u.getName()); s.commit(); s.close(); } catch (IOException e) { e.printStackTrace(); } } @Test public void selectAll(){ try { //添加配置文件(加载类路径) String resources = "mybatis-config.xml"; //加载文件 InputStream inputStream = Resources.getResourceAsStream(resources); //创建会话工厂Builder,相当于连接池 SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream); //通过sf开启会话,相当于打开连接 SqlSession s = sf.openSession(); List<User> users = s.selectList("users.selectAll"); for(User u : users){ System.out.println(u.getName()+":"+u.getAge()); } s.commit(); s.close(); } catch (IOException e) { e.printStackTrace(); } } }
d在数据库中运行脚本:source e:/sql.sql
二、复杂应用
--------------------------------------------
1.准备数据:sql.sql
2.创建java bean
(1)User
package com.it18zhang.mybatisdemo.domain; /** * Created by stone on 2018/9/19. */ public class User { private Integer id ; private String name ; private int age ; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
(2)Order
package com.it18zhang.mybatisdemo.domain; /** *订单类 */ public class Order { private Integer id; private String orderNo; //建立关联关系 private User user ; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getOrderNo() { return orderNo; } public void setOrderNo(String orderNo) { this.orderNo = orderNo; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
(4)Item
package com.it18zhang.mybatisdemo.domain; /** * Created by stone on 2018/9/20. */ public class Item { private Integer id; private String itemName;
//订单项和订单之间的关联查询 private Order order; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; } public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; } }
3.创建Order映射文件
[resource/OrderMapper.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"> <mapper namespace="orders"> <insert id="insert" > insert into orders(orderno,uid) values(#{orderNo},#{user.id}) </insert> </mapper>
4.修改配置文件,添加映射
[resource/mybatis.xml]
<!--引入映射文件--> <mappers> <mapper resource="*Mapper.xml"/> </mappers>