Mybatis官网地址:https://mybatis.org/
1.添加依赖包
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.4</version> </dependency>
2.主配置文件,目录在resources文件夹下
<?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="mysql"> <environment id="mysql"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///mybatis001"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mappers/UserMapper.xml" /> </mappers> </configuration>
主配置文件的文件属性的用法
进一步简化,通过读取属性文件获取数据库链接字符串
读取引用:
<properties resource="jdbc.propeties"/>
类别名的定义
3. mapper配置文件
<?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.xiaoyui.mapper.UserMapper"> <!--id:对应的接口的函数,resultType:为返回值,--> <select id="GetAll" resultType="com.xiaoyui.domain.UserInfo"> select * from user;</select> <!--形参有两种形式#{}或者${}--> <select id="GetFindId" resultType="com.xiaoyui.domain.UserInfo" parameterType="java.lang.Integer"> select * from user where id=#{id};</select> <!--#{username}类的属性名称--> <insert id="Add" parameterType="com.xiaoyui.domain.UserInfo"> <!--插入操作的返回值--> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> select last_insert_id(); </selectKey> insert into user(username,sex) values(#{username},#{sex}); </insert> <update id="Update" parameterType="com.xiaoyui.domain.UserInfo"> update user set username=#{username},sex=#{sex} where id=#{id}; </update> <delete id="Delete" parameterType="java.lang.Integer"> delete from user where id=#{id}; </delete> </mapper>
4.代码
bean
public class UserInfo implements Serializable{ private Integer id; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } private String username; private String sex; }
Dao接口:
public interface UserMapper { public List<UserInfo> GetAll(); public UserInfo GetFindId(int id); public int Add(UserInfo user); public int Update(UserInfo user); public int Delete(int id); }
测试代码如下:
@Test public void testUserGetAll() throws IOException { InputStream inputStream= Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); SqlSession session= sqlSessionFactory.openSession(); UserMapper userMapper=session.getMapper(UserMapper.class); List<UserInfo> list= userMapper.GetAll(); /* 第二种获取实体对象的方式 */ //List<UserInfo> list= session.selectList("com.xiaoyui.mapper.UserMapper.GetAll"); for (UserInfo user : list) { System.out.println(user.getUsername()); } //获取单个 UserInfo current=userMapper.GetFindId(1); //插入 userMapper.Add(new UserInfo(){ }); //修改 current.setUsername("test6"); userMapper.Update(current); //删除 userMapper.Delete(1); //提交事务 session.commit(); session.close(); inputStream.close(); }
5.其他
like的使用
<!--自带百分号,模糊查询 %查%,预编译,防止sql注入--> <select id="find" parameterType="java.lang.String" resultType="com.xiaoyui.domain.UserInfo"> select * from user like #{username} </select> <!--SQL拼接--> <select id="find" parameterType="java.lang.String" resultType="com.xiaoyui.domain.UserInfo"> select * from user like '%${value}%' </select>
resultMap:JavaBean与数据库字段的映射
<resultMap id="userdto" type="com.xiaoyui.domain.UserInfo"> <collection property="id" column="_id" /> </resultMap>
联合查询示例,多对一
联合查询1对多
6.#{}与${}区别
引用类型的时候,两者表达一致。
普通类型时,#是随意值,$必须是value