棉桃剥壳机做网站,wordpress密码忘记了,建立网站谁给你钱,深圳小企业网站建设设计制作目录 
1.MyBatis的简单介绍 
2.MyBatis操作数据库的步骤 
2.1 添加依赖 
2.2 配置文件 
2.3 写持久层代码 
2.4 方法测试 
3.MyBatis操作数据库(增删查改) 
3.1 CRUD标签 
3.2 参数传递 
3.3 Insert-新增 
3.4 Delete-删除 
3.5 Update-修改 
3.6 Select-查询(映射问题)  1.MyB…目录 
1.MyBatis的简单介绍 
2.MyBatis操作数据库的步骤 
2.1 添加依赖 
2.2 配置文件 
2.3 写持久层代码 
2.4 方法测试 
3.MyBatis操作数据库(增删查改) 
3.1 CRUD标签 
3.2 参数传递 
3.3 Insert-新增 
3.4 Delete-删除 
3.5 Update-修改 
3.6 Select-查询(映射问题)  1.MyBatis的简单介绍 
MyBatis是一款持久层框架(持久层指持久化操作的层通常指数据访问层dao)简单来说使用MyBatis能够更简单地完成程序和数据库之间的交互 
2.MyBatis操作数据库的步骤 
2.1 添加依赖 
首先需要导入MyBatis依赖和MySQL驱动依赖(添加到pom.xml文件中) dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion3.0.3/version/dependencydependencygroupIdcom.mysql/groupIdartifactIdmysql-connector-j/artifactIdscoperuntime/scope/dependency 
2.2 配置文件 
我们需要配置Mysql数据库信息MyBatis日志打印和MyBatis XML的文件路径数据库信息用于和指定数据库进行连接MyBatis日志用于查看SQL语句的执行传递的参数以及执行结果(MyBatis日志打印的配置不是必须的如果不需要在配置文件中删除相关配置信息即可)MyBatis XML文件路径(以下面的配置内容为例)用于声明在resources/mapper下创建所有的XML文件 
如果是application.yml文件配置以下内容 
# 配置Mysql数据库信息
spring:datasource:url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncodingutf8useSSLfalse# url中的mybatis_test替换为自己的数据库名username: root    #数据库用户名password: root   #数据库密码(如果没设置密码默认为root如果有的话需要修改例如123456)driver-class-name: com.mysql.cj.jdbc.Driver
# 配置打印 MyBatis⽇志
mybatis:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl# 配置MyBatis XML文件路径mapper-locations: classpath:mapper/**Mapper.xml 
如果是application.properties文件配置以下内容 
# 配置Mysql数据库信息
spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver
spring.datasource.urljdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncodingutf8useSSLfalse
# 上面url中的mybatis_test替换为自己的数据库名
spring.datasource.usernameroot #数据库用户名
spring.datasource.passwordroot #数据库密码(如果没设置密码默认为root如果有的话需要修改例如123456)
# 配置MyBatis日志
mybatis.configuration.log-implorg.apache.ibatis.logging.stdout.StdOutImpl
# 配置MyBatis XML文件路径
mybatis.mapper-locationsclasspath:mapper/**Mapper.xml 
2.3 写持久层代码 
持久层代码分为两个部分分别是方法定义(接口interface)和方法实现(**.xml) 
1方法定义(UserInfoXMLMapper接口) 
Mapper表示MyBatis中的Mapper接口程序运行时会自动生成接口的代理对象并交给Spring的IoC容器管理 
package org.example.mybatisdemo.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.example.mybatisdemo.model.UserInfo;
import java.util.List;
Mapper
public interface UserInfoXMLMapper {ListUserInfo queryUserInfos();
} 
2方法实现(UserInfoXMLMapper.xml根据MyBatis XML文件路径创建路径为resources/mapper/UserInfoXMLMapper.xml) 
mapper标签表示命名空间需要指定namespace属性值为Mapper接口的全限定名(全包名.类名) 
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.mybatisdemo.mapper.UserInfoXMLMapper
!--    sql语句的具体实现--
/mapper 
2.4 方法测试 
package org.example.mybatisdemo.mapper;
import org.example.mybatisdemo.model.UserInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
SpringBootTest //在Spring的运行环境下进行测试
class UserInfoXMLMapperTest {Autowiredprivate UserInfoXMLMapper userInfoXMLMapper;Testvoid queryUserInfos() {//具体测试代码}
} 
3.MyBatis操作数据库(增删查改) 
3.1 CRUD标签 
CRUD标签即insertdeleteselectupdate四个标签用于在方法实现的mapper标签中实现具体的SQL语句以select为例 
select执行数据库的查询操作id与接口中的定义的方法名称相同表示对接口的具体实现方法resultType返回的数据类型值为该类型的全限定名 
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.mybatisdemo.mapper.UserInfoXMLMapperselect idqueryUserInfos resultTypeorg.example.mybatisdemo.model.UserInfoselect * from userinfo/select
/mapper 
3.2 参数传递 
当我们执行条件查询时等SQL语句时需要在SQL语句中对表指定属性赋值这时可以使用#{ }的方式来获取方法中的参数拼接到SQL语句中从而得到完整的SQL语句括号中的参数名称与方法中的参数名称最好相同(当然也可以不相同下面会说到) 
//UserInfoXMLMapper.xml
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.mybatisdemo.mapper.UserInfoXMLMapperselect idqueryUserInfos resultTypeorg.example.mybatisdemo.model.UserInfoselect * from userinfo where id#{id} and age#{age}
--获取id和age参数,分别赋值-最终执行SQL语句为select * from userinfo where id1 and age18/select
/mapper//UserInfoXMLMapper接口
package org.example.mybatisdemo.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.example.mybatisdemo.model.UserInfo;
import java.util.List;
Mapper
public interface UserInfoXMLMapper {ListUserInfo queryUserInfos(UserInfo userInfo);
}//方法测试代码
package org.example.mybatisdemo.mapper;
import org.example.mybatisdemo.model.UserInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
SpringBootTest
class UserInfoXMLMapperTest {Autowiredprivate UserInfoXMLMapper userInfoXMLMapper;Testvoid queryUserInfos() {UserInfo userInfonew UserInfo();userInfo.setId(1);userInfo.setAge(18);userInfoXMLMapper.queryUserInfos(userInfo).forEach(System.out::println);}
} 如果需要使括号中的参数名称与方法中的参数名称不相同可以通过Param设置参数的别名同时使用#{ }获取参数时括号中的参数名称要与别名相同 
//UserInfoXMLMapper接口
package org.example.mybatisdemo.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.example.mybatisdemo.model.UserInfo;
import java.util.List;
Mapper
public interface UserInfoXMLMapper {ListUserInfo queryUserInfos(Param(userid) Integer id);
}//UserInfoXMLMapper.xml
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.mybatisdemo.mapper.UserInfoXMLMapperselect idqueryUserInfos resultTypeorg.example.mybatisdemo.model.UserInfoselect * from userinfo where id#{userid} /select
/mapper 
3.3 Insert-新增 
//UserInfoXMLMapper.xml
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.mybatisdemo.mapper.UserInfoXMLMapperinsert idinsertUserInfoinsert into userinfo (username,password,age) values (#{userName},#{password},#{age})/insert
/mapper//UserInfoXMLMapper接口
package org.example.mybatisdemo.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.example.mybatisdemo.model.UserInfo;
Mapper
public interface UserInfoXMLMapper {Integer insertUserInfo(UserInfo userInfo);
}//方法测试代码
package org.example.mybatisdemo.mapper;
import org.example.mybatisdemo.model.UserInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
SpringBootTest
class UserInfoXMLMapperTest {Autowiredprivate UserInfoXMLMapper userInfoXMLMapper;Testvoid insertUserInfo() {UserInfo userInfo  new UserInfo();userInfo.setUserName(wangliu);userInfo.setPassword(123456);userInfo.setAge(18);userInfoXMLMapper.insertUserInfo(userInfo);}
} 接口定义不变时在XML文件设置useGeneratedKeys和keyProperty属性可以返回自增的表属性 
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.mybatisdemo.mapper.UserInfoXMLMapperinsert idinsertUserInfo useGeneratedKeystrue keyPropertyid--返回自增的id属性insert into userinfo (username,password,age) values (#{userName},#{password},#{age})/insert
/mapper 
3.4 Delete-删除 
//UserInfoXMLMapper.xml
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.mybatisdemo.mapper.UserInfoXMLMapperdelete iddeleteUserInfodelete from userinfo where id#{id}/delete
/mapper//UserInfoXMLMapper接口
package org.example.mybatisdemo.mapper;
import org.apache.ibatis.annotations.Mapper;
Mapper
public interface UserInfoXMLMapper {Integer deleteUserInfo(Integer id);
}//方法测试代码
package org.example.mybatisdemo.mapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
SpringBootTest
class UserInfoXMLMapperTest {Autowiredprivate UserInfoXMLMapper userInfoXMLMapper;Testvoid deleteUserInfo() {userInfoXMLMapper.deleteUserInfo(10);}
} 3.5 Update-修改 
//UserInfoXMLMapper.xml
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.mybatisdemo.mapper.UserInfoXMLMapperupdate idupdateUserInfoupdate userinfo set username#{userName} where id#{id}/update
/mapper//UserInfoXMLMapper类
package org.example.mybatisdemo.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.example.mybatisdemo.model.UserInfo;
Mapper
public interface UserInfoXMLMapper {Integer updateUserInfo(UserInfo userInfo);
}//方法测试代码
package org.example.mybatisdemo.mapper;
import org.example.mybatisdemo.model.UserInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
SpringBootTest
class UserInfoXMLMapperTest {Autowiredprivate UserInfoXMLMapper userInfoXMLMapper;Testvoid updateUserInfo() {UserInfo userInfo  new UserInfo();userInfo.setUserName(zhangliu);userInfo.setId(9);userInfoXMLMapper.updateUserInfo(userInfo);}
} 3.6 Select-查询(映射问题)  
在参数传递(3.2节)已经举了一个关于查询操作的例子这里便不再举例虽然在这个例子中代码能够正常运行但是当我们仔细观察运行结果会发现一些问题。以下是该例子运行结果的截图(与参数传递中的截图一致) 我们会发现有几个属性没有赋值(表中该条数据不存在为null的属性)这是因为自动映射查询结果时MyBatis会获取结果中返回的列名属性并在Java类中查找相同名称的属性(忽略大小写)而delete_flagcreate_time等列名属性与Java类(这里指UserInfo类)中的deleteFlagcreateTime等属性名称不同所以会返回null。这时只需要参照列名属性修改一下Java类中的属性名称就可以得到正确结果当然如果不想修改属性名称有三种方式可以解决这个问题分别是起别名结果映射和开启驼峰别名 
1起别名 
在SQL语句中给列名起别名(as)使别名和Java类中属性名称保持相同接口和方法测试代码与例子中一致 
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.mybatisdemo.mapper.UserInfoXMLMapperselect idqueryUserInfos resultTypeorg.example.mybatisdemo.model.UserInfoselect id,username,password,age,gender,phone,delete_flag as deleteFlag,create_time as createTime,update_time as updateTimefrom userinfo where id#{id} and age#{age}/select
/mapper
!--接口和方法测试代码与例子中一致-- 
2结果映射 
id(resultMap标签)resultMap别名type映射的实体类id标签表示主键column(result标签)数据库列名属性property(result标签)Java类属性名 
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.mybatisdemo.mapper.UserInfoXMLMapperresultMap idXMLBaseMap typeorg.example.mybatisdemo.model.UserInfoid columnid propertyid/idresult columndelete_flag propertydeleteFlag/resultresult columncreate_time propertycreateTime/resultresult columnupdate_time propertyupdateTime/result/resultMapselect idqueryUserInfos resultMapXMLBaseMapselect * from userinfo where id#{id} and age#{age}/select
/mapper
!--接口和方法测试代码与例子中一致-- 
3开启驼峰命名 
数据库列名一般使用蛇形命名法进行命名(下划线分割各个单词)而Java属性一般遵循驼峰命名法约定要在这两种命名方式之间启用自动映射(delete_flag→deleteFlag)需要配置一下配置文件 
如果是application.yml文件配置以下内容 
mybatis:configuration:map-underscore-to-camel-case: true #配置驼峰⾃动转换 
如果是application.properties文件配置以下内容 
#配置驼峰自动转换
mybatis.configuration.map-underscore-to-camel-case: true  文章转载自: http://www.morning.ygxf.cn.gov.cn.ygxf.cn http://www.morning.bkslb.cn.gov.cn.bkslb.cn http://www.morning.rdxnt.cn.gov.cn.rdxnt.cn http://www.morning.dgknl.cn.gov.cn.dgknl.cn http://www.morning.fkmrj.cn.gov.cn.fkmrj.cn http://www.morning.fgkrh.cn.gov.cn.fgkrh.cn http://www.morning.txrq.cn.gov.cn.txrq.cn http://www.morning.qpnmd.cn.gov.cn.qpnmd.cn http://www.morning.nynlf.cn.gov.cn.nynlf.cn http://www.morning.mkbc.cn.gov.cn.mkbc.cn http://www.morning.wxfgg.cn.gov.cn.wxfgg.cn http://www.morning.ktcfl.cn.gov.cn.ktcfl.cn http://www.morning.kltsn.cn.gov.cn.kltsn.cn http://www.morning.pxjp.cn.gov.cn.pxjp.cn http://www.morning.ysjjr.cn.gov.cn.ysjjr.cn http://www.morning.zpdjh.cn.gov.cn.zpdjh.cn http://www.morning.lgwpm.cn.gov.cn.lgwpm.cn http://www.morning.yfddl.cn.gov.cn.yfddl.cn http://www.morning.znrgq.cn.gov.cn.znrgq.cn http://www.morning.qkbwd.cn.gov.cn.qkbwd.cn http://www.morning.lxmks.cn.gov.cn.lxmks.cn http://www.morning.psdsk.cn.gov.cn.psdsk.cn http://www.morning.rpkg.cn.gov.cn.rpkg.cn http://www.morning.byxs.cn.gov.cn.byxs.cn http://www.morning.zbjfq.cn.gov.cn.zbjfq.cn http://www.morning.pnljy.cn.gov.cn.pnljy.cn http://www.morning.lpyjq.cn.gov.cn.lpyjq.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.krrjb.cn.gov.cn.krrjb.cn http://www.morning.xqspn.cn.gov.cn.xqspn.cn http://www.morning.rkwwy.cn.gov.cn.rkwwy.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.kjmws.cn.gov.cn.kjmws.cn http://www.morning.nkiqixr.cn.gov.cn.nkiqixr.cn http://www.morning.ryxyz.cn.gov.cn.ryxyz.cn http://www.morning.hcqd.cn.gov.cn.hcqd.cn http://www.morning.dkbgg.cn.gov.cn.dkbgg.cn http://www.morning.gpnfg.cn.gov.cn.gpnfg.cn http://www.morning.qdzqf.cn.gov.cn.qdzqf.cn http://www.morning.hwycs.cn.gov.cn.hwycs.cn http://www.morning.ryfqj.cn.gov.cn.ryfqj.cn http://www.morning.ztcwp.cn.gov.cn.ztcwp.cn http://www.morning.nfmlt.cn.gov.cn.nfmlt.cn http://www.morning.ybgt.cn.gov.cn.ybgt.cn http://www.morning.bqmhm.cn.gov.cn.bqmhm.cn http://www.morning.zntf.cn.gov.cn.zntf.cn http://www.morning.dzyxr.cn.gov.cn.dzyxr.cn http://www.morning.gwtgt.cn.gov.cn.gwtgt.cn http://www.morning.jpkhn.cn.gov.cn.jpkhn.cn http://www.morning.yptwn.cn.gov.cn.yptwn.cn http://www.morning.bgqqr.cn.gov.cn.bgqqr.cn http://www.morning.ghphp.cn.gov.cn.ghphp.cn http://www.morning.zpqbh.cn.gov.cn.zpqbh.cn http://www.morning.ggnjq.cn.gov.cn.ggnjq.cn http://www.morning.mooncore.cn.gov.cn.mooncore.cn http://www.morning.nkqxb.cn.gov.cn.nkqxb.cn http://www.morning.ypwlb.cn.gov.cn.ypwlb.cn http://www.morning.sbrxm.cn.gov.cn.sbrxm.cn http://www.morning.nwynx.cn.gov.cn.nwynx.cn http://www.morning.ppqzb.cn.gov.cn.ppqzb.cn http://www.morning.sypzg.cn.gov.cn.sypzg.cn http://www.morning.gllgf.cn.gov.cn.gllgf.cn http://www.morning.gmmyn.cn.gov.cn.gmmyn.cn http://www.morning.rwqj.cn.gov.cn.rwqj.cn http://www.morning.jbmsp.cn.gov.cn.jbmsp.cn http://www.morning.prprj.cn.gov.cn.prprj.cn http://www.morning.jxmjr.cn.gov.cn.jxmjr.cn http://www.morning.ktqtf.cn.gov.cn.ktqtf.cn http://www.morning.ykrg.cn.gov.cn.ykrg.cn http://www.morning.rqsnl.cn.gov.cn.rqsnl.cn http://www.morning.prfrb.cn.gov.cn.prfrb.cn http://www.morning.xnlj.cn.gov.cn.xnlj.cn http://www.morning.ptwzy.cn.gov.cn.ptwzy.cn http://www.morning.cflxx.cn.gov.cn.cflxx.cn http://www.morning.rnnwd.cn.gov.cn.rnnwd.cn http://www.morning.bwzzt.cn.gov.cn.bwzzt.cn http://www.morning.zlgr.cn.gov.cn.zlgr.cn http://www.morning.tslwz.cn.gov.cn.tslwz.cn http://www.morning.yzzfl.cn.gov.cn.yzzfl.cn http://www.morning.msmtf.cn.gov.cn.msmtf.cn