创建user表,添加数据
创建模块,导入坐标
4.0.0
com.zxx
MyBatis_Study
1.0-SNAPSHOT
8
8
mysql
mysql-connector-java
5.1.32
org.mybatis
mybatis
3.5.5
junit
junit
4.13
test
org.slf4j
slf4j-api
1.7.36
test
ch.qos.logback
logback-classic
1.2.3
ch.qos.logback
logback-core
1.2.3
编写MyBatis核心配置文件 --> 替换连接信息,解决硬编码问题
编写SQL映射文件 --> 统一管理SQL语句,解决硬编码问题
编码
定义POJO类
package com.zxx.pojo;
public class User {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
加载核心配置文件,获取SqlSessionFactory对象
获取SqlSession对象,执行SQL语句
释放资源
package com.zxx;
import com.zxx.pojo.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 java.io.InputStream;
import java.util.List;
/*
* MyBatis快速入门
*/
public class MyBatis {
public static void main(String[] args) throws Exception {
//1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行SQL
SqlSession sqlSession = sqlSessionFactory.openSession();
//3. 执行SQL
List users = sqlSession.selectList("test.selectAllUser");
System.out.println(users);
//4.释放资源
sqlSession.close();
}
}
定义与SQL映射文件同名的Mapper接口,并且将Mapper接口和SQL的映射文件放到同一个路径下,可以在Resources路径下创建与接口路径相同的结构,需要用 / 分隔,编译后会在同一个目录下
设置SQL映射文件的namespace属性为Mapper接口全限定名
在Mapper接口中定义方法,方法名就是SQL映射文件中的sql语句的id,并保持参数类型和返回值类型一致
package com.zxx.mapper;
import com.zxx.pojo.User;
import java.util.List;
public interface UserMapper {
List selectAllUser();
}
编码
package com.zxx;
import com.zxx.mapper.UserMapper;
import com.zxx.pojo.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 java.io.InputStream;
import java.util.List;
/*
* MyBatis代理开发
*/
public class MyBatis2 {
public static void main(String[] args) throws Exception {
//1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行SQL
SqlSession sqlSession = sqlSessionFactory.openSession();
//3. 执行SQL
// 3.1 获取UserMapper接口的代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List users = userMapper.selectAllUser();
System.out.println(users);
//4.释放资源
sqlSession.close();
}
}
安装MyBatisX插件
package com.zxx.pojo;
public class Brand {
private Integer id;
private String brandName;
private String companyName;
private Integer ordered;
private String description;
private Integer status;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public Integer getOrdered() {
return ordered;
}
public void setOrdered(Integer ordered) {
this.ordered = ordered;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
@Override
public String toString() {
return "Brand{" +
"id=" + id +
", brandName='" + brandName + '\'' +
", companyName='" + companyName + '\'' +
", ordered=" + ordered +
", description='" + description + '\'' +
", status=" + status +
'}';
}
}
id, brand_name as brandName, company_name as companyName,ordered, description, status
INSERT INTO tb_brand ( brand_name, company_name, ordered, description, status )
VALUES
( #{brandName},#{companyName},#{ordered},#{description},#{status}
)
update tb_brand
set brand_name = #{brandName},
company_name = #{companyName},
ordered = #{ordered},
description = #{description},
status = #{status}
where id = #{id}
update tb_brand
brand_name = #{brandName},
company_name = #{companyName},
ordered = #{ordered},
description = #{description},
status = #{status}
where id = #{id}
delete from tb_brand where id = #{id}
delete from tb_brand where id
in(
#{id}
)
package com.zxx.mapper;
import com.zxx.pojo.Brand;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface BrandMapper {
/*
* 查询所有
*/
List selectAll();
/*
* 查看详情
*/
Brand selectById(Integer id);
/*
* 多条件动态查询: 散装参数
*/
List selectByCondition1(@Param("status") int status,@Param("companyName") String companyName,@Param("brandName") String brandName);
/*
* 多条件动态查询: 对象参数,对象属性的名称要和参数占位符名称一致
*/
List selectByCondition2(Brand brand);
/*
* 多条件动态查询:map参数
*/
List selectByCondition3(Map map);
/*
* 单条件动态查询
* */
List selectByConditionSingle(Brand brand);
/*
* 添加一个对象
* */
void add(Brand brand);
/*
* 修改全部字段
* */
int update(Brand brand);
/*
* 动态修改字段
* */
int updateByCondition(Brand brand);
/*
* 根据Id删除
* */
void deleteById(Integer id);
/*
* 批量删除
* */
void deleteByIds(@Param("ids") int[] ids);
}
package com.zxx.mapper;
import com.zxx.pojo.Brand;
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.HashMap;
import java.util.List;
import java.util.Map;
public class MyBatis_Test {
@Test
public void testSelectAll() throws IOException {
//1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行SQL
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
List brands = brandMapper.selectAll();
System.out.println(brands);
//释放资源
sqlSession.close();
}
@Test
public void testSelectById() throws IOException {
//1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行SQL
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
Brand brands = brandMapper.selectById(2);
System.out.println(brands);
//释放资源
sqlSession.close();
}
@Test
public void testSelectByCondition1() throws IOException {
//1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行SQL
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
List brands = brandMapper.selectByCondition1(1, "%华为%", "%华为%");
System.out.println(brands);
//释放资源
sqlSession.close();
}
@Test
public void testSelectByCondition2() throws IOException {
Brand brand = new Brand();
brand.setStatus(1);
brand.setCompanyName("%华为%");
brand.setBrandName("%华为%");
//1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行SQL
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
List brands = brandMapper.selectByCondition2(brand);
System.out.println(brands);
//释放资源
sqlSession.close();
}
@Test
public void testSelectByCondition3() throws IOException {
Map map = new HashMap<>();
map.put("status", 1);
//map.put("companyName","%华为%");
// map.put("brandName","%华为%");
//1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行SQL
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
List brands = brandMapper.selectByCondition3(map);
System.out.println(brands);
//释放资源
sqlSession.close();
}
@Test
public void testSelectByConditionSingle() throws IOException {
Brand brand = new Brand();
//brand.setStatus(1);
//brand.setCompanyName("%华为%");
brand.setBrandName("%华为%");
//1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行SQL
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
List brands = brandMapper.selectByConditionSingle(brand);
System.out.println(brands);
//释放资源
sqlSession.close();
}
@Test
public void testAdd() throws IOException {
Brand brand = new Brand();
brand.setStatus(1);
brand.setCompanyName("波导手机");
brand.setBrandName("波导");
brand.setDescription("手机中的战斗机");
brand.setOrdered(100);
//1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行SQL
//SqlSession sqlSession = sqlSessionFactory.openSession();
//自动提交事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
brandMapper.add(brand);
/*//5.手动提交事务
sqlSession.commit();*/
List brands = brandMapper.selectAll();
System.out.println(brands);
//释放资源
sqlSession.close();
}
@Test
public void testAdd2() throws IOException {
Brand brand = new Brand();
brand.setStatus(1);
brand.setCompanyName("波导手机");
brand.setBrandName("波导");
brand.setDescription("手机中的战斗机");
brand.setOrdered(100);
//1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行SQL
//SqlSession sqlSession = sqlSessionFactory.openSession();
//自动提交事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
brandMapper.add(brand);
Integer id = brand.getId();
System.out.println(id);
/*//5.手动提交事务
sqlSession.commit();*/
List brands = brandMapper.selectAll();
System.out.println(brands);
//释放资源
sqlSession.close();
}
@Test
public void testUpdate() throws IOException {
Brand brand = new Brand();
brand.setStatus(1);
brand.setCompanyName("波导手机");
brand.setBrandName("波导");
brand.setDescription("波导手机,手机中的战斗机");
brand.setOrdered(200);
brand.setId(8);
//1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行SQL
//SqlSession sqlSession = sqlSessionFactory.openSession();
//自动提交事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
brandMapper.update(brand);
//释放资源
sqlSession.close();
}
@Test
public void testupdateByCondition() throws IOException {
Brand brand = new Brand();
brand.setStatus(4);
// brand.setCompanyName("波导手机");
//brand.setBrandName("波导");
//brand.setDescription("波导手机,手机中的战斗机");
//brand.setOrdered(200);
brand.setId(7);
//1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行SQL
//SqlSession sqlSession = sqlSessionFactory.openSession();
//自动提交事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
brandMapper.updateByCondition(brand);
//释放资源
sqlSession.close();
}
@Test
public void testDeleteById() throws IOException {
//1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行SQL
//SqlSession sqlSession = sqlSessionFactory.openSession();
//自动提交事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
brandMapper.deleteById(7);
List brands = brandMapper.selectAll();
System.out.println(brands);
//释放资源
sqlSession.close();
}
@Test
public void testDeleteByIds() throws IOException {
int[] ids = new int[]{5,6};
//1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行SQL
//SqlSession sqlSession = sqlSessionFactory.openSession();
//自动提交事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
brandMapper.deleteByIds(ids);
List brands = brandMapper.selectAll();
System.out.println(brands);
//释放资源
sqlSession.close();
}
}
package com.zxx.mapper;
import com.zxx.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface UserMapper {
List selectAllUser();
User select(@Param("name") String username, @Param("password") String password);
@Select("select * from user where id=#{id}")
User selectUserById(int id);
}
package com.zxx.mapper;
import com.zxx.pojo.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;
public class MyBatis_Test2 {
@Test
public void testUserSelect() throws IOException {
//1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行SQL
//SqlSession sqlSession = sqlSessionFactory.openSession();
//自动提交事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.select("'zxx'","'zxxpass'");
System.out.println(user);
}
@Test
public void testUserSelectUserById() throws IOException {
//1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行SQL
//SqlSession sqlSession = sqlSessionFactory.openSession();
//自动提交事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.selectUserById(1);
System.out.println(user);
}
}