数据库版本:MySQL8.0.19
目录结构:
pom.xml文件:
<?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>org.example</groupId>
<artifactId>mybatis_day2</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<target>1.8</target>
<source>1.8</source>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
domain->address.java文件和dao->IAddressDao.java接口、AddressDao.java实体类
address.java
package domain;
import java.io.Serializable;
public class Address implements Serializable {
private Integer id;
private String des;
@Override
public String toString() {
return "Address{" +
"id=" + id +
", des='" + des + '\'' +
'}';
}
public Integer getId() {return id;}
public void setId(Integer id) {this.id = id;}
public String getDes() {return des;}
public void setDes(String des) {this.des = des;}
}
IAddressDao.java接口
package dao;
import domain.Address;
import java.util.List;
/**
* 持久层
*/
public interface IAddressDao {
List<Address> findAll();
}
AddressDao.java实体类
public class AddressDao implements IAddressDao {
private SqlSessionFactory sqlSessionFactory;
public AddressDao(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
@Override
public List<Address> findAll() {
SqlSession sqlSession = sqlSessionFactory.openSession();
List<Address> list = sqlSession.selectList("dao.IAddressDao.findAll");//参数就是能获取配置信息的key
sqlSession.close();
return list;
}
@Override
public void AddAddress(Address address) {
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.insert("dao.IAddressDao.AddAddress",address);
sqlSession.commit();
sqlSession.close();
}
}
mybatis主配置文件:
<?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">
<!--mybatis主配置文件-->
<configuration>
<!-- 配置环境-->
<!-- 对domain里面的类起别名-->
<typeAliases>
<!-- 用于指定要配置别名的包,当指定以后,该包下面的实体类都会注册别名,并且类名就是别名,不区分大小写,使用的时候是在映射配置文件中使用-->
<package name="domain"/>
</typeAliases>
<!-- default值要和下面的id值相同-->
<environments default="mysql">
<environment id="mysql">
<!-- 配置事务类型-->
<transactionManager type="jdbc"/>
<dataSource type="POOLED">
<!-- 配置数据库的四个基本信息-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<!-- 我用的是MySQL8.0.19社区版,不同的版本,连接语句不一样-->
<property name="url" value="jdbc:mysql://localhost:3306/test2?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="totoro"/>
</dataSource>
</environment>
</environments>
<mappers >
<!--
使用配置文件来完成操作
指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件
<mapper resource="xml/IAddressDao.xml"/>
-->
<!--
如果使用注解来配置的话,此处应该使用class属性指定被注解的dao的全限定类名
mybatis的映射配置文件位置必须和dao接口的包结构相同
<mapper class="dao.IAddressDao"/>
还要在IAddressDao接口里面findall方法上面加上@select注解
public interface IAddressDao {
@Select("select * from address")
List<Address> findAll();
}
-->
<!--
package标签用于指定dao接口所在的包,当指定以后就不用再写mapper以及resource或者class了
mybatis的映射配置文件位置必须和dao接口的包结构相同
-->
<package name="dao"/>
</mappers>
</configuration>
IAddressDao.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属性的取值必须是dao接口的全限定类名
映射配置文件的操作配置(select),id属性的取值必须是dao接口的方法名
-->
<mapper namespace="dao.IAddressDao">
<!--
当Java中的实体类和数据库表中的属性不对应时,解决办法有两个
①:在查询语句的时候起别名的方式来使他们对应,例如:select description as des from address
②:使用resultMap进行对应,这时,select的返回值类型就要修改了,例如:
<resultMap id="addressMap" type="domain.Address">
设置主键对应
<id property="id" column="id"/>
设置非主键对应
<result property="des" column="description"/>
</resultMap>
<select id="findAll" resultMap="addressMap">
select * from address;
</select>
resultMap id 要和select resultMap中对应起来
-->
<!-- 增加一个数据-->
<insert id="AddAddress" parameterType="Address">
insert into address(des)values(#{des});
</insert>
<!-- 更新一个数据-->
<update id="updateA" parameterType="Address">
update address set des=#{des} where id=#{id};
</update>
<!-- 删除一个数据-->
<delete id="deleteA" parameterType="int">
delete from address where id=#{id};
</delete>
<!-- 查询所有数据-->
<select id="findAll" resultType="Address">
select * from address;
</select>
<!-- 查询一个数据-->
<select id="findById" parameterType="int" resultType="Address">
select * from address where id=#{id};
</select>
<!-- 模糊查询,模糊查询的“%”在查询的时候添加上-->
<select id="findbyname" parameterType="string" resultType="Address">
select * from address where des like #{desc};
</select>
<!-- 使用聚合函数-->
<select id="countNum" resultType="int">
select count(*) from address;
</select>
</mapper>
Test测试类:
public class MybatisTest {
@Test
public void findall() throws Exception{
//1.读取配置文件,配置文件是mybatis的全局配置文件,不是单独个配置文件
InputStream inputStream = Resources.getResourceAsStream("SqlMybatisCon.xml");
//2.创建SqlSessionFactory工厂
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
//3.使用工厂生产SqlSession对象
SqlSession sqlSession= sqlSessionFactory.openSession();
//4.使用SqlSession创建Dao接口的代理对象
IAddressDao iAddressDao =sqlSession.getMapper(IAddressDao.class);
//5.使用代理对象执行方法
List<Address> list = iAddressDao.findAll();
for (Address a:list) {
System.out.println(a.toString());
}
inputStream.close();
sqlSession.close();
}
@Test
public void all() throws Exception{
//1.读取配置文件,配置文件是mybatis的全局配置文件,不是单独个配置文件
InputStream inputStream = Resources.getResourceAsStream("SqlMybatisCon.xml");
//2.创建SqlSessionFactory工厂
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
IAddressDao iAddressDao = new AddressDao(sqlSessionFactory);
List<Address> list = iAddressDao.findAll();
for (Address a:list) {
System.out.println(a.toString());
}
inputStream.close();
}
@Test
public void add()throws Exception{
Address address = new Address();
address.setDes("japan");
//1.读取配置文件,配置文件是mybatis的全局配置文件,不是单独个配置文件
InputStream inputStream = Resources.getResourceAsStream("SqlMybatisCon.xml");
//2.创建SqlSessionFactory工厂
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
//3.使用工厂生产SqlSession对象
SqlSession sqlSession= sqlSessionFactory.openSession();
//4.使用SqlSession创建Dao接口的代理对象
IAddressDao iAddressDao =sqlSession.getMapper(IAddressDao.class);
//5.使用代理对象执行方法
iAddressDao.AddAddress(address);
sqlSession.commit();
inputStream.close();
sqlSession.close();
}
@Test
public void insert()throws Exception{
Address address = new Address();
address.setDes("dddddd");
//1.读取配置文件,配置文件是mybatis的全局配置文件,不是单独个配置文件
InputStream inputStream = Resources.getResourceAsStream("SqlMybatisCon.xml");
//2.创建SqlSessionFactory工厂
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
IAddressDao iAddressDao = new AddressDao(sqlSessionFactory);
iAddressDao.AddAddress(address);
inputStream.close();
}
}