一、使用maven加载依赖
加载了连接数据库的依赖、mybatis的依赖以及lombok的依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version> 4.11 </version> <scope>test</scope> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version> 5.1 . 47 </version> </dependency> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version> 3.5 . 4 </version> </dependency> <dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> <version> 1.18 . 12 </version> </dependency> |
二、建库、建表
三、配置mybatis的配置文件(conf.xml)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?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> <environments default = "development" > <environment id= "development" > <transactionmanager type= "jdbc" /> <datasource type= "pooled" > <property name= "driver" value= "com.mysql.jdbc.driver" /> <property name= "url" value= "jdbc:mysql://localhost:3306/数据库名字?/useunicode=true&characterencoding=utf8&usessl=false" /> <property name= "username" value= "root" /> <property name= "password" value= "数据库密码" /> </datasource> </environment> </environments> </configuration> |
四、定义表所对应的实体类
1
2
3
4
5
6
7
8
9
|
@data @noargsconstructor @allargsconstructor public class user { private integer uid; private string uname; private string password; } |
五、定义操作表的sql的映射文件xxxmapper.xml
见名知意,操作的user表,所以映射文件也命名为了usermapper.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?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" > <!--tuser是我自己定的名字--> <mapper namespace= "tuser" > <!--id自己设置,等会调用方法名使用,resulttype查询结果类型--> <select id= "getuserbyid" resulttype= "com.hongda.space.entity.user" > <!--sql语句,#{}是参数格式,注意字段要与实体类属性一致--> select * from t_user where uid=#{uid} </select> <!--parametertype参数类型--> <insert id= "insertuser" parametertype= "com.hongda.space.entity.user" > insert into t_user(uname,password) values(#{uname},#{password}); </insert> </mapper> |
六、在mybatis配置文件中添加映射文件
将编写好的usermapper.xml添加到mybatis.xml配置文件,下方
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?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> <environments default = "development" > <environment id= "development" > <transactionmanager type= "jdbc" /> <datasource type= "pooled" > <property name= "driver" value= "com.mysql.jdbc.driver" /> <property name= "url" value= "jdbc:mysql://localhost:3306/数据库名字?/useunicode=true&characterencoding=utf8&usessl=false" /> <property name= "username" value= "root" /> <property name= "password" value= "数据库密码" /> </datasource> </environment> </environments> <mappers> <mapper resource= "mapper/usermapper.xml" /> </mappers> </configuration> |
七、编写增删改查代码测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
import com.hongda.space.entity.user; 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.reader; /** * @classname test01 * @description todo * @author whd * @date 2021/3/19 14:45 */ public class test01 { public static void main(string[] args) throws ioexception { //1.读取配置文件 reader reader = resources.getresourceasreader( "conf.xml" ); //2.获取sqlsessionfactory sqlsessionfactory sessionfactory = new sqlsessionfactorybuilder().build(reader); //3.获取sqlsession对象 执行sql sqlsession session = sessionfactory.opensession(); //4.执行sql user user = session.selectone( "tuser.getuserbyid" , 1 ); system.out.println(user); int i = session.insert( "tuser.insertuser" , new user( null , "小刘" , "4545" )); system.out.println(i+ "------------" ); session.commit(); } } |
结果:
一些错误
编写中可能碰到的错误
1.mapped statements collection does not contain…
2.the server time zone value'xxxxxxxxx' is…
在mybatis配置文件中的jdbc连接地址后面拼接servertimezone=asia/shanghai
总结
到此这篇关于mybatis新手快速入门以及一些错误的文章就介绍到这了,更多相关mybatis快速入门及错误内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/WHD1998/article/details/115011468