1.类型别名
注意:接口和它的Mapper配置文件最好同名,在同一包下
<typeAliases> <typeAlias type="pojo.User" alias="User"></typeAlias> </typeAliases>
UserMapper.xml就可以直接用User了
<select id="getUserId" resultType="User" parameterType="int"> select * from mybatis_study.user where id= #{id}; </select>
2.结果集映射ResultMap
主要用于解决属性名与数据库表中字段不一致
实体类属性
表字段
运行结果
解决方法:
resultMap结果集映射
UserMapper.xml
3.mybatis使用步骤
1.导入mysql驱动和mybatis、lombok依赖
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> <scope>provided</scope> </dependency> </dependencies>
2.编写mybatis-config.xml配置文件
<?xml version="1.0" encoding="UTF8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!--mybatis的主配置文件--> <configuration> <!--导入外部配置文件,properties必须放在最上边--> <properties resource="db.properties"> <!--里面可以继续写属性,但是没外部文件的优先级高--> <property name="username" value="root"/> </properties> <!--日志--> <settings> <!--<setting name="logImpl" value="STDOUT_LOGGING"/>--> <setting name="logImpl" value="LOG4J"/> </settings> <!--可以给实体类起别名--> <typeAliases> <typeAlias type="pojo.Teacher" alias="Teacher"></typeAlias> <typeAlias type="pojo.Student" alias="Student"></typeAlias> </typeAliases> <!--配置环境--> <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 class="mapper.TeacherMapper"></mapper> <mapper class="mapper.StudentMapper"></mapper> </mappers> </configuration>
3.db.properties文件
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis_study?serverTimezome=UTC username=root password=123456
4.工具类utils下的MybatisUtil.Java
public class MybatisUtil { private static SqlSessionFactory factory; static{ try { //1.读取配置文件 InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); //2.创建SqlsessionFactory工厂 factory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ //3.使用工厂生产sqlsession对象 return factory.openSession(); } }
5.pojo实体类User.java
@Data public class User{ private Integer id; private String name; private String sex; private String address; }
6.对应Mapper接口
public interface UserMapper { //查询全部用户 List<User> findAll(); //根据ID查找 User getUserId(int id); //添加用户 int addUser(User user); //更新用户 int updateUser(User user); //删除用户 int deleteUser(int id); }
7.对应的Mapper.xml文件
<mapper namespace="mapper.UserMapper"> <!--配置查询所有--> <!--id对应的是dao层的方法名--> <select id="findAll" resultType="domain.User"> select * from user; </select> <!--根据id的查询--> <select id="getUserId" resultType="domain.User" parameterType="int"> select * from mybatis_study.user where id= #{id}; </select> <!--添加操作--> <insert id="addUser" parameterType="domain.User"> insert into user (id,username,sex,address) values (#{id},#{username},#{sex},#{address}); </insert> <!--更新操作--> <update id="updateUser" parameterType="domain.User"> update user set username=#{username},sex=#{sex},address=#{address} where id = #{id}; </update> <!--删除操作--> <delete id="deleteUser" parameterType="int"> delete from user where id = #{id} </delete> </mapper>
8.测试类
//查询所有 @Test public void TestList(){ SqlSession session = MybatisUtil.getSqlSession(); UserMapper userMapper = session.getMapper(UserMapper.class); List<User> list = userMapper.findAll(); for (User user : list) { System.out.println(user); } //释放资源 session.close(); } //根据ID查询 @Test public void getUserId(){ SqlSession session = MybatisUtil.getSqlSession(); UserMapper mapper = session.getMapper(UserMapper.class); User userId = mapper.getUserId(41); System.out.println(userId); session.close(); } //添加操作,增删改都需要提交事务 @Test public void addUser(){ SqlSession session = MybatisUtil.getSqlSession(); UserMapper mapper = session.getMapper(UserMapper.class); mapper.addUser(new User(3, "小哈", "男", "新乡市")); //提交事务 session.commit(); session.close(); } //更新操作 @Test public void updateUser(){ SqlSession session = MybatisUtil.getSqlSession(); UserMapper mapper = session.getMapper(UserMapper.class); mapper.updateUser(new User(3,"小何","女","河南省")); //提交事务 session.commit(); session.close(); } //删除操作 @Test public void deleteUser(){ SqlSession session = MybatisUtil.getSqlSession(); UserMapper mapper = session.getMapper(UserMapper.class); mapper.deleteUser(3); session.commit(); session.close(); }