1.初步搭建Mybatis
1.创建maven工程
2.引入依赖
<dependencies>
<!-- Mybatis核心 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.3</version>
</dependency>
</dependencies>
3.创建Mybatis核心配置文件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:3306/MyBatis"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--引入映射文件-->
<mappers>
<mapper resource="mappers/UserMapper.xml"/>
</mappers>
</configuration>
4.创建mapper接口
public interface CustomerMapper {
List<Customer> selectAll();
int insert(Customer customer);
Customer selectById(int id);
int updateRelNameById(Customer customer);
int deleteById(int id);
List<Customer> selectByCondition(Customer condition);
}
5.创建mybatis映射文件
<?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="com.ytsky.mybatis.demo.dao.CustomerMapper">
<select id="selectAll" resultType="Customer">
select id, name, tel, address, rel_name, other
from db_quote.tb_customer
</select>
<select id="selectById" resultType="Customer">
select id, name, tel, address, rel_name , other
from db_quote.tb_customer
where id = #{id}
</select>
<select id="selectByCondition" parameterType="Customer" resultType="Customer">
select id, name, tel, address, rel_name, other
from db_quote.tb_customer
<where>
<if test="name != null">
and name = #{name}
</if>
<if test="tel != null">
and tel = #{tel}
</if>
<if test="address != null">
and address like #{address}
</if>
<if test="relName != null">
and rel_name = #{relName}
</if>
<if test="other != null">
and other like #{other}
</if>
</where>
</select>
<insert id="insert" parameterType="Customer">
insert into db_quote.tb_customer( name, tel, address, rel_name, other)
VALUES(#{name},#{tel},#{address},#{relName},#{other})
</insert>
<update id="updateRelNameById" parameterType="Customer">
update db_quote.tb_customer
set rel_name= #{relName}
where id= #{id}
</update>
<delete id="deleteById" parameterType="int">
delete from db_quote.tb_customer where id = #{id}
</delete>
</mapper>
6. 加入log4J日志功能
1)加入依赖
<!-- log4j日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2) 加入 log4j 的配置文件
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--设置 mybatis在运行时-->
<setting name="logImpl" value="LOG4J"/>
</settings>
2.Mybatis的增删查改
1.添加数据
<insert id="insert" parameterType="Customer">
insert into db_quote.tb_customer( name, tel, address, rel_name, other)
VALUES(#{name},#{tel},#{address},#{relName},#{other})
</insert>
2.删除数据
<delete id="deleteById" parameterType="int">
delete from db_quote.tb_customer where id = #{id}
</delete>
3.查询数据
<select id="selectAll" resultType="Customer">
select id, name, tel, address, rel_name, other
from db_quote.tb_customer
</select>
<select id="selectById" resultType="Customer">
select id, name, tel, address, rel_name , other
from db_quote.tb_customer
where id = #{id}
</select>
4.修改数据
<update id="updateRelNameById" parameterType="Customer">
update db_quote.tb_customer
set rel_name= #{relName}
where id= #{id}
</update>
3.Mybatis获取数据的方式
1.MyBatis 获取参数值的两种方式:${}和 #{}
2.${}的本质就是字符串拼接,#{}的本质就是占位符赋值
3.${}使用字符串拼接的方式拼接 sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单引号;但是 #{}使用占位符赋值的方式拼接sql.此时为字符串类型或日期类型的字段进行赋值时,可以自动添加单引号