mybatis配置文件解析
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/15001033f7f58f78c9be06793d57fd68.png#pic_center)
package com.bjpowernode.Dao;
import com.bjpowernode.domain.Student;
import com.bjpowernode.vo.StudentAndClassroomVo;
import java.util.List;
import java.util.Map;
public interface StudentDao {
List<Student> getAll() ;
public Student getById(String id);
public void save(Student s);
public Student selete1(String a0001);
List<Student> selete2(int i);
List<Student> selete3(String cxk, int i);
List<Student> selete4();
List<Student> selete5(Map<String, Object> map);
Student selete6(String a0002);
List<Student> selete7(String z);
List<Student> selete8(String z);
List<Student> selete9(String s);
String selete10(String a0002);
List<String> selete11();
int selete12();
List<Map<String, Object>> select14();
List<Student> selete15();
List<Student> selete16();
List<Student> selete17();
List<Student> selete18(String[] strArr);
Student selete19(String a0002);
List<Map<String, Object>> selete20();
List<StudentAndClassroomVo> selete21();
List<StudentAndClassroomVo> selete22(String name);
}
<?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">
<!--namespace:命名空间
不同的mapper文件用namespace区分
不同的mapper映射文件所适用的namespace的命名不允许出现重复
使用命名空间.sqlId的形式来找到我们想要的执行的sql语句
parameterType:sql语句传递的参数类型
-->
<mapper namespace="com.bjpowernode.Dao.StudentDao">
<select id="getAll" resultType="Student">
select * from tbl_student
</select>
<select id="getById" parameterType="java.lang.String" resultType="com.bjpowernode.domain.Student">
select * from tbl_student where id=#{id}
</select>
<insert id="save">
insert into tbl_student(id,name,age) values(#{id},#{name},#{age})
</insert>
<!--parameter:java.lang.String,String,string都可以写-->
<select id="selete1" parameterType="String" resultType="Student">
select * from tbl_student where id=#{id}
</select>
<!--使用简单类型(8大基本类型)为参数
在#{}里的参数随意写-->
<select id="selete2" parameterType="int" resultType="Student">
select * from tbl_student where age=#{age123343}
</select>
<!--绝对不可以为sql语句传递多个参数,我们想要传递多个参数时,我们应该将多个参数封装到domain中或者是打包到map集合中-->
<!--<select id="selete3" parameterType="" resultType="Student">
select * from tbl_student where age=#{age},name=#{name}
</select>-->
<!--如果我们为参数传递的是个Domain类型,我们的#{}中必须传递的是属性名-->
<select id="selete4" parameterType="Student" resultType="Student">
select * from tbl_student where name=#{name} and age=#{age}
</select>
<!--如果我们为sql语句传递一个map类型,那么#{}中必须是key-->
<select id="selete5" parameterType="Student" resultType="Student">
select * from tbl_student where name=#{name} and age=#{age}
</select>
<!--
#{}:表示占位符,可以有效地方式sql注入,使用#{}设置参数无需考虑参数的类型。preparedStatement
${}:表示拼接符,不能方式sql注入,使用${}设置参数必须考虑参数类型,statement
有时候需要动态拼接表名
Select * from ${tablename}
String tbl="tbl_student";
String sql="select * from"+sql;
动态拼接排序字段
select * from tablename order by ${username} desc
-->
<select id="selete6" resultType="Student">
select * from tbl_student where id='${value}';
</select>
<!--like模糊查询 使用${}执行like模糊查询
使用#{}执行like模糊查询-->
<select id="selete7" resultType="Student">
select * from tbl_student where name like '%${value}%'
</select>
<!--在mysql中空格相当于+,必须加,不能省略-->
<select id="selete8" resultType="Student">
select * from tbl_student where name like '%' #{name} '%'
</select>
<select id="selete10" resultType="String">
select name from tbl_student where id=#{id}
</select>
<select id="selete11" resultType="String">
select name from tbl_student
</select>
<select id="selete12" resultType="int">
select count(*) from tbl_student
</select>
<select id="select14" resultType="map">
select * from tbl_student
</select>
<!--起别名的方式-->
<select id="selete15" resultType="Student">
select id,
fullname as name ,
age from tbl_student
</select>
<!--
id:resultMap标签对的唯一标识
将来在使用resulrMap标签的时候,使用id来找到这组标签
type:指定一个类型,与数据库一一对应,建立表字段和类属性的名字一一匹配的关系
-->
<resultMap id="stuMap" type="Student">
<!--
id标签:用来配置主键的对应关系的
result标签:用来配置普通字段对应关系的
对于tbl_student,表结果时一个id,两个普通的字段
我们需要一个id标签,两个result标签
property属性:配置的是类中的属性名
column:配置都是表中的字段名
-->
<id property="id" column="id"/>
<result property="name" column="fullname"/>
<result property="age" column="age"/>
</resultMap>
<select id="selete16" resultMap="stuMap">
select * from tbl_student;
</select>
<select id="selete17" parameterType="Student" resultType="Student">
select * from tbl_student
<where>
<if test="name!=null and name!=''">
name like '%' #{name} '%'
</if>
<if test="address!=null and address!=''">
and address like '%' #{address} '%'
</if>
</where>
</select>
<select id="selete18" resultType="Student">
select * from tbl_student
where id in
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
<!--使用sql标签制作sql片段
sql片段的作用是代替sql语句中代码
如果你的mapper映射文件中的sql语句某些代码出现了大量的重复,我们可以使用sql片段来代替他们
id:sql片段的唯一标识,将来找到sql片段使用id来进行定位
一般情况下没有必要使用sql片段的必要
大量使用sql片段会大大降低sql语句可读性
-->
<sql id="sql1">
select * from tbl_student
</sql>
<select id="selete19" resultType="Student">
<include refid="sql1"/> where id=#{id}
</select>
<!--c.name和s.name不能一样所以只能起别名-->
<select id="selete20" resultType="map">
select s.name as sname,
c.name as cname
from tbl_student s
join tbl_classroom c
on s.classroomId=c.id
</select>
<select id="selete21" resultType="com.bjpowernode.vo.StudentAndClassroomVo">
select
s.id as sid,
s.name as sname,
s.age sage,
s.address saddress,
c.id cid,
c.name cname
from tbl_student s
join tbl_classroom c
on s.classroomId=c.id
</select>
<select id="selete22" resultType="com.bjpowernode.vo.StudentAndClassroomVo">
select
s.id as sid,
s.name as sname,
s.age sage,
s.address saddress,
c.id cid,
c.name cname
from tbl_student s
join tbl_classroom c
on s.classroomId=c.id
where s.name like '%' #{sname} '%'
</select>
</mapper>
// An highlighted block
package com.bjpowernode.domain;
public class Student {
private String id;
private String name;
private Integer age;//Integer可以表现空值
private String address;
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.bjpowernode.service.Impl;
import com.bjpowernode.Dao.StudentDao;
import com.bjpowernode.domain.Student;
import com.bjpowernode.service.StudentService;
import com.bjpowernode.util.SqlSessionUtil;
import java.util.List;
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao= SqlSessionUtil.getSession().getMapper(StudentDao.class);
@Override
public Student getById(String id) {
Student s=studentDao.getById(id);
return s;
}
@Override
public void save(Student s) {
studentDao.save(s);
}
@Override
public List<Student> getAll() {
List<Student> slist=studentDao.getAll();
return slist;
}
}
package com.bjpowernode.service;
import com.bjpowernode.domain.Student;
import java.util.List;
public interface StudentService {
public Student getById(String id);
public void save(Student s);
List<Student> getAll();
}
package com.bjpowernode.test;
import com.bjpowernode.domain.Student;
import com.bjpowernode.service.Impl.StudentServiceImpl;
import com.bjpowernode.service.StudentService;
import com.bjpowernode.util.ServiceFactory;
import java.util.List;
public class test1 {
public static void main(String[] args) {
StudentService ss= (StudentService) ServiceFactory.getService(new StudentServiceImpl());
Student s=new Student();
s.setAge(25);
s.setId("A0005");
s.setName("cxl");
ss.save(s);
Student s1=ss.getById("A0002");
System.out.println(s1);
List<Student> s11=ss.getAll();
for (Student sss:s11){
System.out.println(sss);
}
}
}
package com.bjpowernode.test;
import com.bjpowernode.Dao.StudentDao;
import com.bjpowernode.domain.Student;
import com.bjpowernode.util.SqlSessionUtil;
import com.bjpowernode.vo.StudentAndClassroomVo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class test2 {
public static void main(String[] args) {
StudentDao studentDao=SqlSessionUtil.getSession().getMapper(StudentDao.class);
List<StudentAndClassroomVo> voList=studentDao.selete22("z");
for (StudentAndClassroomVo slist:voList){
System.out.println(slist);
}
}
}
package com.bjpowernode.util;
public class ServiceFactory {
public static Object getService(Object service){
return new TransactionInvocationHandler(service).getProxy();
}
}
package com.bjpowernode.util;
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;
public class SqlSessionUtil {
private static SqlSessionFactory sqlSessionFactory;
private SqlSessionUtil(){}
static {
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
}
private static ThreadLocal<SqlSession> t = new ThreadLocal<>();
public static SqlSession getSession() {
SqlSession session = t.get();
if (session == null) {
session = sqlSessionFactory.openSession();
t.set(session);
}
return session;
}
public static void MyClose(SqlSession session) {
if (session != null) {
session.close();
t.remove();
}
}
}
package com.bjpowernode.util;
import org.apache.ibatis.session.SqlSession;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class TransactionInvocationHandler implements InvocationHandler {
private Object target;
public TransactionInvocationHandler(Object target){
this.target=target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
SqlSession session=null;
Object obj=null;
try {
session = SqlSessionUtil.getSession();
obj=method.invoke(target,args);
session.commit();
}catch (Exception e){
session.rollback();
e.printStackTrace();
}finally {
SqlSessionUtil.MyClose(session);
}
return obj;
}
public Object getProxy(){
return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
}
}
package com.bjpowernode.vo;
public class StudentAndClassroomVo {
private String sid;
private String sname;
private Integer sage;
private String saddress;
private String cid;
private String cname;
@Override
public String toString() {
return "StudentAndClassroomVo{" +
"sid='" + sid + '\'' +
", sname='" + sname + '\'' +
", sage=" + sage +
", saddress='" + saddress + '\'' +
", cid='" + cid + '\'' +
", cname='" + cname + '\'' +
'}';
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Integer getSage() {
return sage;
}
public void setSage(Integer sage) {
this.sage = sage;
}
public String getSaddress() {
return saddress;
}
public void setSaddress(String saddress) {
this.saddress = saddress;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
}
<?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="db.properties" />
<!--设置与数据库交互的环境
配置二级缓存,配置查询延迟策略
配置的目的是为了更加有效的查询表中的记录
在实际项目中setting基本没用,因为settings对于查询的优化基本上没有效果
如何提高查询效率
基础操作
对于常用的查询条件的字段,设置索引
高级操作
使用nosql数据库,redis
专业操作
Elasticsearch>Solr
针对于电商行业
-->
<!--<settings>
<setting name="" value=""/>
</settings>-->
<!--mapper中映射文件为domain起别名-->
<typeAliases>
<!--
方式一:为指定的类分别起别名
type:要为哪个domain起别名,填写包名,类名
alias:别名名字
方式二:使用package标签批量起别名
别名是Mybatis替我们起好的,命名不是由我们自己决定的,别名为类名(类名的名字不区分大小写)
name指定一个包结构
-->
<package name="com.bjpowernode.domain"/>
<!-- <typeAlias type="com.bjpowernode.domain.Student" alias="stu"></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>
<!--方式一:resource是找到全路径-->
<!--<mapper resource="com\bjpowernode\Dao\StudentDao.xml"/>-->
<!--方式二:
使用class属性,找到dao层接口的全路径-->
<!-- <mapper class="com.bjpowernode.Dao.StudentDao"></mapper>-->
<!--方式三
批量注册
name:指向dao层的包,表示在该包下所有的mapper映射文件自动注册-->
<package name="com.bjpowernode.Dao"/>
</mappers>
</configuration>