给一个企业做网站,wordpress国外主题汉化,制作软件的工作叫什么,安徽弘泰建设管理有限公司网站文章目录 1.基础使用1.添加依赖2.在resouces文件下新建xml文件db.properties3.在resouces文件下新建xml文件mybatis-config-xml4.创建一个MybatisUtils工具类5.创建xml文件XxxMapper.xml映射dao层接口6.添加日志5.测试 2.增删改查1.select2.delete3.update4.insert5.模糊查询6.… 文章目录 1.基础使用1.添加依赖2.在resouces文件下新建xml文件db.properties3.在resouces文件下新建xml文件mybatis-config-xml4.创建一个MybatisUtils工具类5.创建xml文件XxxMapper.xml映射dao层接口6.添加日志5.测试 2.增删改查1.select2.delete3.update4.insert5.模糊查询6.分页查询 3.起别名3.1具体的某个文件3.2给包名起别名3.3用注解起别名 4.解决实体属性名与数据库列名不一致问题1.建一个resultMap标签2.引用 5.使用注解5.1在接口上写注解5.2进行绑定 6.association和collection6.1一对多6.2多对一 7.动态查询7.1模糊查询if标签7.2更新数据set标签7.3Forech 8.二级缓存8.1在mybatis-config.xml中开启全局缓存8.1添加局部缓存在xxMapper.xml中添加 1.基础使用
1.添加依赖
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.18/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/version/dependency!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --dependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.4.6/version/dependencybuild
resourcesresourcedirectorysrc/main/resources/directoryincludesinclude**/*.properties/includeinclude**/*.xml/include/includesfilteringfalse/filtering/resourceresourcedirectorysrc/main/java/directoryincludesinclude**/*.properties/includeinclude**/*.xml/include/includesfilteringfalse/filtering/resource
/resources
/build2.在resouces文件下新建xml文件db.properties
写配置文件
drivercom.mysql.cj.jdbc.Driver
urljdbc:mysql://localhost:3306/mybatis?serverTimezoneAsia/ShanghaiuseSSLtrueuseUnicodetruecharacterEncodingutf-8
usernameroot
passwordDRsXT5ZJ6Oi55LPQ3.在resouces文件下新建xml文件mybatis-config-xml
?xml version1.0 encodingUTF-8 ?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd
configurationproperties resourcedb.properties/environments defaultdevelopmentenvironment iddevelopmenttransactionManager typeJDBC/dataSource typePOOLEDproperty namedriver value${driver}/property nameurl value${url}/property nameusername value${username}/property namepassword value${password}//dataSource/environment/environmentsmappersmapper resourcecom/tuzhi/dao/UserMapper.xml//mappers
/configuration4.创建一个MybatisUtils工具类
public class MybatisUtils {private static SqlSessionFactory sqlSessionFactory;static {String resource org/mybatis/example/mybatis-config.xml;InputStream inputStream null;try {inputStream Resources.getResourceAsStream(resource);} catch (IOException e) {e.printStackTrace();}sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);}public SqlSession getSqlSession() {return sqlSessionFactory.openSession();}
}5.创建xml文件XxxMapper.xml映射dao层接口
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
!--映射dao层接口--
mapper namespacecom.tuzhi.dao.UserDao
!-- 映射接口里面的方法--select idgetUserList resultTypecom.tuzhi.pojo.Userselect * from user/select
/mapper6.添加日志
settingssetting namelogImpl valueLOG4J/!-- 是否开启驼峰命名自动映射即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn。--setting namemapUnderscoreToCamelCase valuetrue/!-- 开启全局缓存--setting namecacheEnabled valuetrue//settings5.测试
Testpublic void test() {SqlSession sqlSession MybatisUtils.getSqlSession();UserDao userDao sqlSession.getMapper(UserDao.class);ListUser userList userDao.getUserList();for (User user : userList) {System.out.println(user);}sqlSession.close();}2.增删改查
1.select
select idgetUserById resultTypecom.tuzhi.pojo.User parameterTypeintselect * from user where id #{id}/select2.delete
delete iddeleteUser parameterTypecom.tuzhi.pojo.Userdeletefrom USERwhere id #{id};/delete3.update
update idupdateUser parameterTypecom.tuzhi.pojo.Userupdate USERset name #{name},pwd #{pwd}where id #{id};/update4.insert
insert idaddUser parameterTypecom.tuzhi.pojo.Userinsert into USER (id,name ,pwd)values (#{id},#{name},#{pwd});/insert5.模糊查询
select idgetUserListLike resultTypecom.tuzhi.pojo.Userselect * from user where name like concat(%,#{name},%)/select6.分页查询
!-- 分页查询--select idgetUserLimit parameterTypemap resultMapuserResultMapselect * from user limit #{startIndex},#{pageSize}/select3.起别名
3.1具体的某个文件
typeAliasestypeAlias aliasAuthor typedomain.blog.Author/typeAlias aliasBlog typedomain.blog.Blog/typeAlias aliasComment typedomain.blog.Comment/typeAlias aliasPost typedomain.blog.Post/typeAlias aliasSection typedomain.blog.Section/typeAlias aliasTag typedomain.blog.Tag/
/typeAliases3.2给包名起别名
typeAliasespackage namedomain.blog/
/typeAliases注用别名的时候直接用文件名全小写
3.3用注解起别名
Alias(author)注直接在类上注解
4.解决实体属性名与数据库列名不一致问题
1.建一个resultMap标签
resultMap iduserResultMap typeUser//property实体类里的column数据库里的id propertyid columnuser_id /result propertyusername columnuser_name/result propertypassword columnhashed_password/
/resultMap2.引用
然后在引用它的语句中设置 resultMap 属性就行了注意我们去掉了 resultType 属性。比如:
select idselectUsers resultMapuserResultMapselect user_id, user_name, hashed_passwordfrom some_tablewhere id #{id}
/select5.使用注解
5.1在接口上写注解
public interface UserMapper {// 使用注解Select(select * from user)ListUser getUserListAnnotate();
}5.2进行绑定
mappersmapper classcom.tuzhi.dao.UserMapper/
/mappers6.association和collection
association用于对象关联
collection用于集合
6.1一对多 实体类 Data
AllArgsConstructor
NoArgsConstructor
public class Student {private int id;private String name;private Teacher teacher;
} Data
AllArgsConstructor
NoArgsConstructor
public class Teacher {private int id;private String name;
}第一种查询 !-- 第一种多对一查询--
select idgetUserList1 resultMapstudentTeacher1select * from student
/select
resultMap idstudentTeacher1 typeStudentassociation propertyteacher columntid selectgetTeacherListById/
/resultMap
select idgetTeacherListById resultTypeTeacherselect * from teacher where id #{tid}
/select第二种查询 !-- 第二种多对一查询--
select idgetUserList2 resultMapstudentTeacher2select s.id sid,s.name sname,t.id tid,t.name tnamefrom student s,teacher twhere s.tid t.id
/select
resultMap idstudentTeacher2 typeStudentresult propertyid columnsid/result propertyname columnsname/association propertyteacher javaTypeTeacherresult propertyid columntid/result propertyname columntname//association
/resultMap6.2多对一 实体类 Data
AllArgsConstructor
NoArgsConstructor
public class Student {private int id;private String name;
} Data
AllArgsConstructor
NoArgsConstructor
public class Teacher {private int id;private String name;private ListStudent student;
} 第一种查询 !-- 第一种查询--select idgetTeacherListById1 resultMapteacherStudent1select t.id id,t.name tname,s.id sid,s.name sname,s.tid tidfrom teacher t,student swhere t.ids.tid/selectresultMap idteacherStudent1 typeTeacherresult propertyid columnid/result propertyname columntname/collection propertystudent ofTypeStudentresult propertyid columnsid/result propertyname columnsname//collection/resultMap第二种查询 !-- 第二种查询--select idgetTeacherListById2 resultMapteacherStudent2select * from teacher where id #{id}/selectresultMap idteacherStudent2 typeTeachercollection propertystudent javaTypeArraylist ofTypeStudent columnid selectgetStudentList//resultMapselect idgetStudentList resultTypeStudentselect * from student where tid #{id}/select7.动态查询
7.1模糊查询if标签
接口
//查询
ListBlog getBlogIf(Map map);if
!-- 动态sql模糊查询--
select idgetBlogIf parameterTypemap resultTypeblogselect * from blogwhereif testtitle ! nulland title like concat(%,#{title},%)/ifif testauthor ! nulland author like concat(%,#{author}.%)/if/where/select7.2更新数据set标签 接口 set标签 !-- 动态更新数据--
update idupdateBlog parameterTypeBlogupdate blogsetif testtitle ! nulltitle #{title},/ifif testauthor ! nullauthor #{author},/ifif testviews ! nullviews #{views},/if/setwhere id #{id}
/update7.3Forech forech select idqueryForeach parameterTypemap resultTypeBlogselect * from blogwhereforeach collectionids itemid openand ( separatoror close)id #{id}/foreach/where
/select测试 Test
public void queryForech() {SqlSession sqlSession MybatisUtils.getSqlSession();BlogMapper mapper sqlSession.getMapper(BlogMapper.class);ArrayList arrayList new ArrayList();arrayList.add(1);arrayList.add(2);HashMap hashMap new HashMap();hashMap.put(ids,arrayList);mapper.queryForeach(hashMap);sqlSession.close();
}8.二级缓存
8.1在mybatis-config.xml中开启全局缓存
setting namecacheEnabled valuetrue/8.1添加局部缓存在xxMapper.xml中添加
cacheevictionFIFOflushInterval60000size512readOnlytrue/