MyBatis
ResultMap
为了解决key不同所带来的错误出现,使用resultmap进行设置映射。但是如果足够了解的情况下,只需要使用最为简单的result,即改动的column 与 property进行使用。∴所以在使用resultmap的条件下,替换掉resulttype进行属性的设置是可行的。
<resultMap id = “” type = "">
<result column = "id" property = "id">
<result column = "pwd" property = "password">
</resultMap>
<select id = "" resultMap= "">
select * from where id = #{id}
</select>
mybatis-config
在对于文件的配置过程中,的配置需要注意属性的配置顺序。
<properties resouce = "db.properties"></properties> <!--对于外部文件引用,即可在对于<environment>中对于datasouce属性进行设置,需注意的是,外部引用优先级大于environment的内部设置-->
<settings>
<setting name = "logImpl" value = ""></setting>
</settings>
<!--对于设置的出现,使用日志进行对于sql实现的过程出现错误进行反馈-->
一定要注意不要出现不该出现的问题,例如名字的问题,空格问题,不要出现
<typeAliases>
<package name =""></package>
</typeAliases>
<!--使用JAVABEAN进行设置,引入pojo层中User的属性设置,get,set,toString方法,同样在dao层中使用@Alias("user"),进行更改兼容,其实就是为了避免名字错误所导致的问题-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value=""/>
<property name="url" value=""/>
<property name="username" value=""/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<!--在environments中,通过default进行环境类型的设置,注意一定要注意到自身所需使用的环境进行default更改,这是相对于内部进行的设置-->
<mappers>
<mapper resource="com/sanhuo/dao/UserMapper.xml"/>
</mappers>
<!--在使用mappers时,注意一定不要使用com.sanhuo.dao这样的形式-->
在mappers的配置中,因为config位于resources目录下,那么使用过滤器进行设置,可以避免相关文件路径的找不到的问题
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
三层配置:
在dao层中,一般不适用直接进行使用class进行User的实现,往往是通过对于使用接口的形式,进行jdbc的替代。所以我们可以得出,在dao层中建设了UserMapper与UserMapper.xml两个文件。
List<User> getUserlist(); //UserMapper
<mapper namespace="">
<select id="getUserlist" resultType="">
select * from books.book;
</select>
</mapper>
使用UserMapper.class
Connection connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
String sql = "SELECT * FROM `book`";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
System.out.println(" " + resultSet.getObject("name"));
System.out.println(" " + resultSet.getObject("id"));
}
resultSet.close();
statement.close();
connection.close();
在pojo层中,即使用User实体类进行相关属性配置。
public class User {
private int id;
private String name;
public User() {
}
public User(int id, String name) {
this.id = id;
this.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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
在utils层中,为了得到一个会话,即session
首先通过设置 new SqlSessionFactoryBuilder().build一个sqlsessionfactory,得到一个工厂,通过工厂进行生产出sqlsession会话
那么就出现了一个问题:我们该怎样设定这样一个会话,即我们改用什么样式的模板来进行会话的生产,这时就需要我们对于resources目录下的mybatis-config.xml的使用
既然我们实现了这样一个config的设置,那么一个io流的文件上传实现即可完成
所需要注意的是,为了避免资源的浪费,与避免高并发的出现,factory在生产过session后,就将factory关闭,而session则将在整个资源获得后进行手动的关闭
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e){
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
分页:
limit:
select * from book limit numindex,pagenum
@Test
public void getUserByLimit(){
SqlSession sqlSession = MybitesUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
HashMap<String, Integer> map = new HashMap<>();
map.put("startIndex",0);
map.put("pageSize",2);
List<User> userlist = mapper.getUserByLimit(map);
for (User user : userlist) {
System.out.println(user);
}
sqlSession.close();
}
RowBounds:
RowBounds rowBounds = new RowBounds(1, 2);
List<Object> objects = sqlSession.selectList("com.sanhuo.dao.UserDao", null, rowBounds);
对于以上的分页方式,其不同于limit形式,为直接使用java代码的形式,获得相关数据信息。
注解使用:
不同于上文出现过的使用接口并且单独实现配置进行sql查询,在使用注解的过程中,已经没有了UserMapper.xml形式的出现,直接为使用接口形式进行sql语句。
@Select("Select * from books.book")
List<User> getUserlist();
这样的解决方法虽好,但带来了一个问题,就是我们将如何代入参数来使用where的相关查询:
@Select("select * from books.book where id = #{id}")
List<User> getUserlist(@Param("id") int id);
那么同样来说:假如在使用@Param()的时候,里面的相关内容与#{}里面的相关内容并不一致,那么错误的出现便不可避免。就如上文我想要使用兼容性来解决命名方式的问题,使用映射的方法来扩展,道理同样可以适用于这里。
与此同时,对于增删改查,这些语句实现的过程中,同样为使用sql语句进行相关实现 @Insert,@Delete,@Update,@Select
额外话题:
在进行自动提交的相关的事物的过程中:
sqlSessionFactory.openSession(true);这也是在注解使用过程中的相关要求
多项查询:
<select id = "getTeacher" resultMap = "TeacherStudent">
select from where
</select>
<resultMap id = "TeacherStudent" type = "Teacher">
<result property = "id" column = "tid"></result>
<result property = "name" column = "tname"></result>
<collection property = "student" ofType = "Student">
<result property = "id" column = "sid"></result>
<result property = "name" column = "sname">
</collection>
</resultMap>
单独查询使用association 多项查询 collection
使用泛型:ofType
<select id = "getTeacher" resultMap = "TeacherStudent">
select * from teacher where id = #{tid}
</select>
<resultMap id = "TeacherStudent" type = "Teacher">
<collection property = "students" javaType = "ArrayList" ofType = "student"></collection>
</resultMap>
<select id = "getTeacher" resultType = "TeacherStudent">
select * from teacher where tid = #{tid}
</select>
动态查询:
if:
<select id = "getTeacher" resultMap = "TeacherStudent">
select * from teacher where id = #{tid}
<if test = "title !=null">
and title = #{title}
</if>
<if test = "author !=null">
and author = #{author}
</if>
</select>
otherwise:
<choose>
<when test = "title != null">
and title like #{title}
</when>
<when test = "author != null" and author != null>
and author_name like #{author,name}
</when>
<otherwise>
and featured = 1
</otherwise>
</choose>
update:
<update>
update author
<set>
<if test = "username != null">
username = #{username},
</if>
<if test = "password != null">
password = #{password},
</if>
<if test = "email != null">
email = #{email}
</if>
</set>
where id = #{id}
</update>
where set:替代设置
<trim prefix = "WHERE" prefixOverride = "AND|OR">
</trim>
<trim prefix = "SET" suffixOverrides = ",">
</trim>
总结:所谓动态处理sql语句,就是将sql语句的相关形式标签化,在这里就体现于where and 等。
缓存
为了应对越发严峻的读写操作,于是在服务器与数据库之间添加缓存以减少对于数据库的操作,避免并发形式的出现。
一级缓存:即上文所出现的sqlsession会话的整个生命周期,从opensession到sqlseaaion.close()整个时间段。所以对于一级缓存来说它是自动进行设置与开启的。
既然有了自动开启,那必然有手动开启这种形式。二级缓存就是这样,其基于某一接口的形式进行相关设置。在二级缓存中,可以通过Cache()接口进行自行定义。
相对于一级缓存来说,二级缓存的生命周期相对较大,为了实现这一功能,可以在mybits-config中进行操作
如果只是默认配置
<cache/>
如果为自行定义,如数据进出方式FIFO,刷新时间等等,这些都可为自行定义。
<cache></cache>
期望对您有所帮助。点赞关注,是您对我最大的鼓励