一、mybatis框架
mybatis框架主要就是完成持久层的实现,简化了持久层的开发,
1.首先是配置文件的编写,我们这里就命名为mybatis-config.xml,先配置文件头,然后加载连接数据库的配置propersties文件,为实体类取别名省的后续在返回值的时候仍使用包名.类名的方式,下边可以设置映射级别,然后再配置数据库的连接,下边的mappers的标签中配置每一个dao接口的对应的mapper文件(注意这里使用有三种方式 class、resource)package标签的name的属性配置指定文件包下的映射文件,
<?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> <properties resource="dev-db.properties"></properties> <!--配置别名--> <typeAliases> <package name="com.song.pojo"/> </typeAliases> <!--配置数据库环境--> <environments default="devlopment"> <environment id="devlopment"> <transactionManager type="jdbc"></transactionManager> <!--连接池--> <dataSource type="pooled"> <property name="username" value="${username}"/> <property name="password" value="${pwd}"/> <property name="url" value="${url}"/> <property name="driver" value="${driver}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/song/dao/Igrade.xml"></mapper> </mappers> </configuration>
2.映射文件,这里一般命名为***mapper.xml文件,再idea的位置是resource文件目录下,创建于dao接口同名同级的文件目录,然后导入文件头(文件约束),mapper标签中的namespace属性填写上其对应dao接口的路径,下边写每一个查询语句,select 返回数据类型可以是全限定的实体类名,也可以是前边配置的别名,这里如果是数据库的列名和实体类的字段名一致,mybatis就可以为你进行自动的映射,如果有 一对一,一对多,多对多的映射关系,就需要手动的去指定其对应关系,resultmap标签,collection一对多 association标签 多对多关系映射 ,传入的参数的属性可以不写,但是返回的值的类型就必须要写上,dml语句的返回就是int类型,所以他没有返回值的属性,这里要说下复杂的动态的SQL语句,需要用到where标签,if标签,拼接字符串用concat(‘%‘,#{id},'%')foreach遍历等
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" > <mapper namespace="com.wgg.dao.AdminDao"> <!--查询所有的用户 --> <select id="getAllAdmin" resultType="com.wgg.pojo.Admin"> select * from admin </select> <!--根据名字来查 我们在写查询语句的时候 ?表示占位符 在mybatis中 就用#{}来表示占位 而且名字最好和传入的参数一致 2.给返回值类型在配置文件中起别名 以简化这个字符串的书写 --> <select id="getAdminByName" resultType="Admin"> select * from admin where username=#{name} </select> <!-- 根据id和名字来查 此时的参数 有两个 可以将parameterType 定义写成对象的类型 他会自动映射 传入的username=#{username}要和 表中的字段一致 --> <select id="getAdminByAdmin" resultType="Admin" parameterType="Admin"> select * from admin where username=#{username} and id=#{id} </select> <!-- 分开传入两个字符串的参数来查询 --> <select id="getAdminByNameAndId" resultType="Admin"> select * from admin where username=#{username} and id=#{id} </select> <!-- 模糊查询 --> <select id="getAdminWithPnameLike" resultType="Admin"> select * from admin where username like CONCAT('%',#{pname},'%') </select> <!-- 传入一个数组作为参数 用来子查询 --> <select id="getAdminWithArray" resultType="Admin"> select * from admin where id in <foreach collection="array" open="(" separator="," close=")" item="idarr"> #{idarr} </foreach> </select> <!-- 传入一个集合来遍历集合查询 --> <select id="getAdminWithIdList" resultType="Admin"> select * from admin where id in <foreach collection="list" open="(" separator="," close=")" item="idList"> #{idList} </foreach> </select> <!-- map集合 --> <select id="getAdminWithMap" resultType="Admin"> select * from admin where username=#{username} and password=#{password} </select> <!-- 增删改 --> <!-- 增加对象信息 传入一个对象--> <insert id="addAdmin"> insert into admin (username,password)values(#{username},#{password}) </insert> <!-- 删除 对象信息 传入一个id--> <delete id="delAdminById"> delete from admin where id=#{id} </delete> <!--修改用户的信息 传入一个string --> <update id="UpdateAdmin"> update admin set username =#{username} where id=#{id} </update> <!-- 多条件查询 动态sql 相当于 多条件的模糊查询--> <select id="getAllAdminInfoByFazzy" resultType="Admin"> select * from admin <!-- 你可以使用where标签来解决这个问题,where 元素只会在至少有一个子元素的条件返回 SQL 子句的情况下才去插入“WHERE”子句。而且,若语句的开头为“AND”或“OR”,where 元素也会将它们去除。 --> <trim prefix="where" prefixOverrides="and | or"> <if test="username!=null"> and username=#{username}</if> <if test="password!=null">and password=#{password}</if> </trim> </select> <!-- Switch --> <select id="getAllAdminInfoBySwitch"> select * from admin <choose> <!-- 进入一个when其他的when 就不再进入了 --> <when test="username!=null">and username=#{username}</when> <when test="password!=null">and password=#{password}</when> <!-- 查询所有 --> <otherwise></otherwise> </choose> </select> </mapper>
3.关于dao的接口,因为mybatis框架,所以我们不用再写复杂的实现类,只需要编写接口就行了,再向mapper文件传参数的时候,如果只有一个参数就和占位符中的名称相同就可以了,多个参数的话,就需要用到对象,集合或者map来传输数据,数组,此时需要遍历取值的就要使用 如:foreach collection="array" 这样的指定容器的类型,然后遍历。如果多个不同的参数可以使用map来指定,只要将他的键指定成占位符中的名称一致就行了,如果传入不同的参数也可以,再dao的接口传参的地方用@parma()注解指定每一个参数,但这样太麻烦,还是用map集合吧,
// 根据id和name来查询对象 public Admin getAdminByNameAndId(@Param("username") String name, @Param("id") int id); // 模糊查询 public Admin getAdminWithPnameLike(String pname); // 传入数组作为参数来查询 public List<Admin> getAdminWithArray(int[] idarr); // 传入集合作为参数来查询 public List<Admin> getAdminWithIdList(List<Integer> idList); // public Admin getAdminWithMap(Map<String, String> idmap); public int addAdmin(Admin admin); public int delAdminById(int id); //修改用户 public int UpdateAdmin(@Param("username")String username, @Param("id")Integer id); //模糊多条件查询 public List<Admin> getAllAdminInfoByFazzy(@Param("username")String username,@Param("password")Integer password); //Switch public List<Admin> getAllAdminInfoBySwitch(@Param("username")String username,@Param("password")Integer password);
4.导入依赖,mybatis用到的jar包不多
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!-- 数据库依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency>
这基本是就是mybatis的框架的基本书写流程了