学习了3种使用方式
案例链接(包含jar包): https://pan.baidu.com/s/1hykf9d2IEFoEPl8XlMWaqQ 提取码: m342
方式一:
WebContent/WEB-INF/lib里面记得放jar包
步骤1:在src下新建一个xml文件SqlMapConfig.xml(核心xml文件)
SqlMapConfig.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:3306/cz" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- 对象映射,这里映射了一个com.hello.pojo包下的Car.xml文件-->
<mappers>
<mapper resource="com/hello/pojo/Car.xml" />
</mappers>
</configuration>
步骤2:在com.hello.pojo包下新建一个Car类,里面的字段和数据库对应表的字段名一样,有有参构造器无参构造器set/get方法
package com.hello.pojo;
import java.io.Serializable;
public class Car implements Serializable {
private Integer id;
private String name;
private Double price;
public Car() {
super();
}
public Car(String name, Double price) {
super();
this.name = name;
this.price = price;
}
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 Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [id=" + id + ", name=" + name + ", price=" + price + "]";
}
}
步骤3:在com.hello.pojo包下新建xml文件Car.xml
Car.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:命名空间,其实没什么大作用,类似分包, 避免出现同名冲突,起到一个 SQL 隔离的作用 -->
<mapper namespace="aa">
<!-- 更新:要有一个新的数据,才能更新 -->
<update id="updateCar" parameterType="com.hello.pojo.Car">
update car set name = #{name}, price = #{price} where id = #{id}
</update>
<!-- 删除 -->
<delete id="deleteCar" parameterType="java.lang.Integer">
delete from car where id = #{id}
</delete>
<!-- 添加 -->
<insert id="addCar" parameterType="com.hello.pojo.Car">
insert into car(name, price) values (#{name}, #{price})
</insert>
<!-- 模糊查询,在 MyBatis 中,返回值类型默认是会自动转成集合形式的。 -->
<select id="findCarByName" parameterType="java.lang.String" resultType="com.hello.pojo.Car">
select * from car where name like '%${value}%'
</select>
<!-- id:给 SQL 语句起名字,以后这个名字会被当做方法名
parameterType:参数类型
resultType:返回值类型
#{xx}:接收参数的表达式,作为 SQL 语句的占位符-->
<!-- 按照id查找-->
<select id="findCarById" parameterType="java.lang.Integer" resultType="com.hello.pojo.Car">
select * from car where id = #{id}
</select>
</mapper>
步骤4:新建com.hello.test包并新建一个测试类CarTest
3个关键步骤:
// 1. 加载核心配置文件
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
// 2. 获取 SqlSessionFactory
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(is);
// 3. 获取 SqlSession
SqlSession session = factory.openSession();
然后就可以通过session调用Car.xml中的各个方法,例如:
删除一个id为1的car:
session.delete("aa.deleteCar", 1); //aa为Car.xml里面的其中一个命名空间,上面我们只有一个命名空间aa
session.commit();//重要的一个地方!!记得提交,增删改操作都需要提交!!
网友评论