使用mybatis时会有哪些基础错误

  介绍

这篇文章主要介绍使用mybatis时会有哪些基础错误,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

一、使用maven加载依赖

加载了连接数据库的依赖,mybatis的依赖以及lombok的依赖

& lt; dependency>   & lt;才能groupId> junit</groupId>   & lt;才能artifactId> junit</artifactId>   & lt;才能version> 4.11 & lt;/version>   & lt;才能scope> test</scope>   ,& lt;/dependency>      ,& lt; dependency>   & lt;才能groupId> mysql</groupId>   & lt;才能artifactId> mysql-connector-java</artifactId>   & lt;才能version> 5.1.47</version>   ,& lt;/dependency>      ,& lt; dependency>   & lt;才能groupId> org.mybatis</groupId>   & lt;才能artifactId> mybatis</artifactId>   & lt;才能version> 3.5.4</version>   ,& lt;/dependency>   ,& lt; dependency>   & lt;才能groupId> org.projectlombok</groupId>   & lt;才能artifactId> lombok</artifactId>   & lt;才能version> 1.18.12</version>   ,& lt;/dependency>

二、建库、建表

使用mybatis时会有哪些基础错误

三,配置mybatis的配置文件(参看。xml)

& lt; ? xml  version=?.0“,编码=癠TF-8" ?比;   & lt; ! DOCTYPE  configuration  PUBLIC “-//mybatis.org//DTD  Config  3.0//EN"   ,,,“http://mybatis.org/dtd/mybatis-3-config.dtd"的在   & lt; configuration>   ,& lt; environments 默认=癲evelopment"比;   & lt;才能environment  id=癲evelopment"比;   ,,& lt; transactionManager 类型=癑DBC",/比;   ,,& lt; dataSource 类型=癙OOLED"比;   ,,,& lt; property  name=癲river",价值=https://www.yisu.com/zixun/" com.mysql.jdbc.Driver "/>   <属性名=" url " value=" jdbc: mysql://localhost: 3306/数据库名字?/useUnicode=true& characterEncoding=utf8& useSSL=false "/>   <属性名="用户名" value="根"/>   <属性名="密码" value="数据库密码"/>                  

四、定义表所对应的实体类

@ data   @NoArgsConstructor   @AllArgsConstructor   public  class  User  {   ,private  Integer  uid;   ,private  String  uname;   ,private  String 密码;      }

五、定义操作表的sql的映射文件xxxMapper.xml

<强>见名知意,操作的用户表,所以映射文件也命名为了usermap。xml

& lt; ? xml  version=?.0“,编码=癠TF-8", ?比;   & lt; ! DOCTYPE  mapper  PUBLIC “-//mybatis.org//DTD  mapper  3.0//EN"   ,,,“http://mybatis.org/dtd/mybatis-3-mapper.dtd"的在   & lt; !——摘要是我自己定的名字——比;=& lt; mapper 名称空间“tuser"祝辞   ,& lt; !——id自己设置,等会调用方法名使用,resultType查询结果类型——比;   ,& lt; select  id=癵etUserById", resultType=癱om.hongda.space.entity.User"比;   & lt;才能!——sql语句,#{}是参数格式,注意字段要与实体类属性一致——比;   select  *,才能得到t_user  where  uid=# {uid}   ,& lt;/select>   ,& lt; !——parameterType参数类型——比;   ,& lt; insert  id=癷nsertUser", parameterType=癱om.hongda.space.entity.User"比;   insert 才能;into  t_user (uname、密码),值(# {uname}, #{密码});   ,& lt;/insert>   & lt;/mapper>

六,在Mybatis配置文件中添加映射文件

将编写好的userMapper.xml添加到Mybatis。xml配置文件,下方

& lt; ? xml  version=?.0“,编码=癠TF-8" ?比;   & lt; ! DOCTYPE  configuration  PUBLIC “-//mybatis.org//DTD  Config  3.0//EN"   ,,,“http://mybatis.org/dtd/mybatis-3-config.dtd"的在   & lt; configuration>   ,& lt; environments 默认=癲evelopment"比;   & lt;才能environment  id=癲evelopment"比;   ,,& lt; transactionManager 类型=癑DBC",/比;   ,,& lt; dataSource 类型=癙OOLED"比;   ,,,& lt; property  name=癲river",价值=https://www.yisu.com/zixun/" com.mysql.jdbc.Driver "/>   <属性名=" url " value=" jdbc: mysql://localhost: 3306/数据库名字?/useUnicode=true& characterEncoding=utf8& useSSL=false "/>   <属性名="用户名" value="根"/>   <属性名="密码" value="数据库密码"/>               

使用mybatis时会有哪些基础错误