一,mybatis映射文件的学习。简化开发步骤。
- 配置数据库 pom文件数据库的操作。
- 配置User文件进行数据库字段的关联
package com.liuboss.demo;public class User { private int id; private String name; @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
- 配置resources文件下的资源。log4j文件,sql操作文件,sql配置文件,resources下com.liuboss.demo文件进行数据库的操作。
<?xml version="1.0" encoding="UTF-8" ?>/span> PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.liuboss.mapper.UserMapper"> <select id="findByCount" resultType="user" parameterType="user"> select * from t_class where id=#{id} and name=#{name} select>mapper>
jdbc.driver = com.mysql.cj.jdbc.Driverjdbc.url = jdbc:mysql://localhost:3306/text01?serverTimezone=UTCjdbc.username = rootjdbc.password = 123456
<?xml version="1.0" encoding="UTF-8" ?>/span> PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <properties resource="jdbc.properties">properties> <typeAliases> <typeAlias type="com.liuboss.demo.User" alias="user">typeAlias> typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> dataSource> environment> environments> <mappers> <mapper resource="com\liuboss\demo\UserMapper.xml">mapper> mappers>configuration>
注意:数据库语句操作文件中,配置的路径名要是全路径。
4.数据库的mapper接口
package com.liuboss.mapper;import com.liuboss.demo.User;import java.util.List;public interface UserMapper { public ListfindByCount(User user);}
5.数据库的测试文件。
package com.liuboss.test;import com.liuboss.demo.User;import com.liuboss.mapper.UserMapper;import com.sun.corba.se.spi.servicecontext.UEInfoServiceContext;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Test;import java.io.IOException;import java.io.InputStream;import java.util.List;public class MapperTest { @Test public void test1() throws IOException { User con = new User(); con.setId(60); con.setName("朱皇帝"); InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.findByCount(con); System.out.println(userList); System.out.println(con); }}
6.可以在sql操作配置文件中进行数据的条件查询
<?xml version="1.0" encoding="UTF-8" ?>/span> PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.liuboss.mapper.UserMapper"> <select id="findByCount" resultType="user" parameterType="user"> select * from t_class <where> <if test="id!=0"> and id=#{id} if> <if test="name!=null"> and name=#{name} if> where> select>mapper>
二,动态sql的使用,重点if的使用,foreach的使用。
方法步骤与上面全部一样,不同只有sql查询语句和测试文件不同。重点内容
<?xml version="1.0" encoding="UTF-8" ?>/span> PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.liuboss.mapper.UserMapper"> <select id="findByCount" resultType="user" parameterType="user"> select * from t_class <where> <if test="id!=0"> and id=#{id} if> <if test="name!=null"> and name=#{name} if> where> select> <select id="findByIds" parameterType="list" resultType="user"> select * from t_class <where> <foreach collection="list" open="id in(" close=")" item="id" separator=","> #{id} foreach> where> select>mapper>
package com.liuboss.test;import com.liuboss.demo.User;import com.liuboss.mapper.UserMapper;import com.sun.corba.se.spi.servicecontext.UEInfoServiceContext;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Test;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;public class MapperTest { @Test public void test1() throws IOException { User con = new User();// con.setId(60);// con.setName("朱皇帝"); InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(2); List<User> userList = mapper.findByIds(ids); System.out.println(userList); }}
7.sql片段的抽取,
<?xml version="1.0" encoding="UTF-8" ?>/span> PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.liuboss.mapper.UserMapper"> <sql id="selectUser"> select * from user sql> <select id="findByCount" resultType="user" parameterType="user"> <include refid="selectUser">include> <where> <if test="id!=0"> and id=#{id} if> <if test="name!=null"> and name=#{name} if> where> select> <select id="findByIds" parameterType="list" resultType="user"> select * from t_class <where> <foreach collection="list" open="id in(" close=")" item="id" separator=","> #{id} foreach> where> select>mapper>
三,Mybatis核心配置文件---自定义属性的类型设置
源码文件:Test06
---重点,当mybatis中没有自己想要的;诶性标签时,将要进行自定义标签类型进行属性的注入和设置。
---重点内容实现内容:自定义属性的类型。
- 数据库
-
user表的配置进行属性的配置。
---在数据库中添加数据
package com.liuboss.demo;import java.util.Date;public class User { private int id; private String name; private String password; private Date birthday; @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", password='" + password + '\'' + ", birthday=" + birthday + '}'; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
2.定义数据库的操作文件及相关的配置文件。
UserMapper.xml数据库额操作文件,注意数据库的全类名。在resources文件下创建com/liuboss/demo文件下创建Usermapper.xml
<?xml version="1.0" encoding="UTF-8" ?>/span> PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.liuboss.mapper.UserMapper"> <insert id="save" parameterType="user"> insert into t_class values(#{id},#{name},#{password},#{birthday}) insert> <select id="findById" parameterType="int" resultType="user"> select * from t_class where id=#{id} select>mapper>
3.数据库的连接文件。注意下面的配置。注册类型处理器的使用方式 重点强调
<configuration> <properties resource="jdbc.properties">properties> <typeAliases> <typeAlias type="com.liuboss.demo.User" alias="user">typeAlias> typeAliases> <typeHandlers> <typeHandler handler="com.liuboss.handler.DateTypeHandler"/> typeHandlers>
<?xml version="1.0" encoding="UTF-8" ?>/span> PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <properties resource="jdbc.properties">properties> <typeAliases> <typeAlias type="com.liuboss.demo.User" alias="user">typeAlias> typeAliases> <typeHandlers> <typeHandler handler="com.liuboss.handler.DateTypeHandler"/> typeHandlers> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> dataSource> environment> environments> <mappers> <mapper resource="com\liuboss\demo\UserMapper.xml">mapper> mappers>configuration>
4.引入lig4j。和jdbc.properties配置文件
5.定义handler文件夹,DateTypeHandler.java文件夹,将所有的数据文件进行,其中继承extends BaseTypeHandler<Date>方法方法,实现其中的方法,
package com.liuboss.handler;import org.apache.ibatis.type.BaseTypeHandler;import org.apache.ibatis.type.JdbcType;import javax.annotation.Resources;import java.sql.CallableStatement;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Date;public class DateTypeHandler extends BaseTypeHandler<Date> { //将java类型转换成数据库需要的类型 public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException { long time = parameter.getTime(); ps.setLong(i,time); } //将数据库中类型转换成java类型 public Date getNullableResult(ResultSet rs, String s) throws SQLException { long aLong = rs.getLong(s); Date date = new Date(aLong); return date; } //将数据库中类型转换成java类型 public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException { long aLong = rs.getLong(columnIndex); Date date = new Date(aLong); return date; } //将数据库中类型转换成java类型 public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { long aLong = cs.getLong(columnIndex); Date date = new Date(aLong); return date; }}
7.定义测试文件
import com.liuboss.demo.User;import com.liuboss.mapper.UserMapper;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;import java.io.InputStream;import java.nio.Buffer;import java.util.Date;public class Test { @org.junit.Test public void test1() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); /*创建user*/ User user = new User(); user.setId(10); user.setName("刘庆东"); user.setBirthday(new Date()); user.setPassword("123456"); mapper.save(user); sqlSession.commit(); sqlSession.close(); } @org.junit.Test public void test2() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User byId = mapper.findById(10); System.out.println(byId.getBirthday()); sqlSession.commit(); sqlSession.close(); }}
三,进行查询全部数据的配置改动的地方代码。
package com.liuboss.mapper;import com.liuboss.demo.User;import com.sun.javafx.collections.VetoableListDecorator;import java.util.List;public interface UserMapper { public void save(User user); public User findById(int id); //查询全部实现功能 public ListfindAll();}
<?xml version="1.0" encoding="UTF-8" ?>/span> PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.liuboss.mapper.UserMapper"> <insert id="save" parameterType="user"> insert into t_class values(#{id},#{name},#{password},#{birthday}) insert> <select id="findById" parameterType="int" resultType="user"> select * from t_class where id=#{id} select> <select id="findAll" resultType="user"> select * from t_class select>mapper>
import com.liuboss.demo.User;import com.liuboss.mapper.UserMapper;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import sun.misc.FormattedFloatingDecimal;import java.io.IOException;import java.io.InputStream;import java.nio.Buffer;import java.util.Date;import java.util.List;public class Test { @org.junit.Test public void test1() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); /*创建user*/ User user = new User(); user.setId(10); user.setName("刘庆东"); user.setBirthday(new Date()); user.setPassword("123456"); mapper.save(user); sqlSession.commit(); sqlSession.close(); } @org.junit.Test public void test2() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User byId = mapper.findById(10); System.out.println(byId.getBirthday()); sqlSession.commit(); sqlSession.close(); } /*查询全部的数据*/ @org.junit.Test public void test3() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.findAll(); for (User user : userList) { System.out.println(user); } sqlSession.close(); }}