mybatis入门_动力节点Java学院整理

网友投稿 264 2023-04-09


mybatis入门_动力节点Java学院整理

本文为大家分享了mybatis入门学习资料,供大家参考,具体内容如下

所需要用到的其他工具或技术:

项目管理工具 : Maven

测试运行工具 : Junit

数据库 : Derby

废话不多说,直接代码

Maven Dependencies:

org.mybatis

mybatis

3.2.7

junit

junit

4.9

test

org.apache.derby

derby

10.10.2.0

org.apache.derby

derbyclient

10.10.2.0

SQL 建表及数据插入:

CREATE TABLE USER_TEST_TB(

ID INT PRIMARY KEY,

USERNAME VARCHAR(20) NOT NULL,

PASSWORD VARCHAR(20) NOT NULL,

NICKNAME VARCHAR(20) NOT NULL

);

INSERT INTO USER_TEST_TB VALUES(1,'1st','111','Jack');

INSERT INTO USER_TEST_TB VALUES(2,'2nd','222','Rose');

INSERT INTO USER_TEST_TB VALUES(3,'3rd','333','Will');

Mybatis配置文件 src/main/resource源目录下

test-mybatis-configuration.xml

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

value="jdbc:derby://localhost:1527/freud;create=true" />

UserMapper.xml  Mapper文件(src/main/java/com.freud.practice目录下)

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select *

from USER_TEST_TB

User.Java对象类(src/main/java/com.freud.practice目录下)

package com.freud.practice;

public class User

{

private Integer id;

private String username;

private String password;

private String nickname;

public Integer getId()

{

return id;

}

public void setId(Integer id)

{

this.id = id;

}

public String getUsername()

{

return username;

}

public void setUsername(String username)

{

this.username = username;

}

public String getPassword()

{

return password;

}

public void setPassword(String password)

{

this.password = password;

}

public String getNickname()

{

return nickname;

}

public void setNickname(String nickname)

{

this.nickname = nickname;

}

}

UserMapper.java Mapper类(src/main/java/com.freud.practice目录下)

package com.freud.practice;

import java.util.List;

import org.apache.ibatis.annotations.Select;

public interface UserMapper

{

//@Select("SELECT * FROM USER_TEST_TB")

public List getUser();

}

测试类TestMyBatis.java(src/test/java/com.freud.practice目录下)

package com.freud.practice;

import java.io.InputStream;

import java.text.MessageFormat;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import org.junit.Before;

import org.junit.Test;

public class TestMyBatis

{

private String source;

private InputStream inputStream;

private SqlSessionFactory sqlSessionFactory;

@Before

public void setUp()

{

source = "test-mybatis-configuration.xml";

}

@Test

public void testXMLConfingureSessionFactory()

{

try

{

inputStream = TestMyBatis.class.getClassLoader().getResourceAsStream(source);

sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

SqlSession session = sqlSessionFactory.openSession();

List users = (List) session.selectList("com.freud.practice.UserMapper.getUser");

System.out.println("Query by XML configuration...");

this.printUsers(users);

UserMapper userMapper = session.getMapper(UserMapper.class);

System.out.println("\r\nQuery by annotation configuration...");

this.printUsers(userMapper.getUser());

}

catch (Exception e)

{

e.printStackTrace();

}

}

private void printUsers(final List users)

{

int count = 0;

for (User user : users)

{

System.out.println(MessageFormat.format("==User[{0}]=================", ++count));

System.out.println("User Id: " + user.getId());

System.out.println("User UserName: " + user.getUsername());

System.out.println("User Passhttp://word: " + user.getPassword());

System.out.println("User nickname: " + user.getNickname());

}

}

}

以上是使用XML作为Mapper配置文件的一个入门,其实Mybatis还支持Annotation的方式,具体操作如下:

1. 打开UserMapper.java中的注释。

2. 打开test-mybatis-configuration.xml中的注释,并注释掉现有的Mapper

3. 删除UserMapper.xml文件

这种是Mybatis的Annotation方式的Mapper配置。个人比较偏向于XML方式,所以后续的教程会更倾向于XML配置的方式来写。

value="jdbc:derby://localhost:1527/freud;create=true" />

UserMapper.xml  Mapper文件(src/main/java/com.freud.practice目录下)

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select *

from USER_TEST_TB

User.Java对象类(src/main/java/com.freud.practice目录下)

package com.freud.practice;

public class User

{

private Integer id;

private String username;

private String password;

private String nickname;

public Integer getId()

{

return id;

}

public void setId(Integer id)

{

this.id = id;

}

public String getUsername()

{

return username;

}

public void setUsername(String username)

{

this.username = username;

}

public String getPassword()

{

return password;

}

public void setPassword(String password)

{

this.password = password;

}

public String getNickname()

{

return nickname;

}

public void setNickname(String nickname)

{

this.nickname = nickname;

}

}

UserMapper.java Mapper类(src/main/java/com.freud.practice目录下)

package com.freud.practice;

import java.util.List;

import org.apache.ibatis.annotations.Select;

public interface UserMapper

{

//@Select("SELECT * FROM USER_TEST_TB")

public List getUser();

}

测试类TestMyBatis.java(src/test/java/com.freud.practice目录下)

package com.freud.practice;

import java.io.InputStream;

import java.text.MessageFormat;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import org.junit.Before;

import org.junit.Test;

public class TestMyBatis

{

private String source;

private InputStream inputStream;

private SqlSessionFactory sqlSessionFactory;

@Before

public void setUp()

{

source = "test-mybatis-configuration.xml";

}

@Test

public void testXMLConfingureSessionFactory()

{

try

{

inputStream = TestMyBatis.class.getClassLoader().getResourceAsStream(source);

sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

SqlSession session = sqlSessionFactory.openSession();

List users = (List) session.selectList("com.freud.practice.UserMapper.getUser");

System.out.println("Query by XML configuration...");

this.printUsers(users);

UserMapper userMapper = session.getMapper(UserMapper.class);

System.out.println("\r\nQuery by annotation configuration...");

this.printUsers(userMapper.getUser());

}

catch (Exception e)

{

e.printStackTrace();

}

}

private void printUsers(final List users)

{

int count = 0;

for (User user : users)

{

System.out.println(MessageFormat.format("==User[{0}]=================", ++count));

System.out.println("User Id: " + user.getId());

System.out.println("User UserName: " + user.getUsername());

System.out.println("User Passhttp://word: " + user.getPassword());

System.out.println("User nickname: " + user.getNickname());

}

}

}

以上是使用XML作为Mapper配置文件的一个入门,其实Mybatis还支持Annotation的方式,具体操作如下:

1. 打开UserMapper.java中的注释。

2. 打开test-mybatis-configuration.xml中的注释,并注释掉现有的Mapper

3. 删除UserMapper.xml文件

这种是Mybatis的Annotation方式的Mapper配置。个人比较偏向于XML方式,所以后续的教程会更倾向于XML配置的方式来写。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:详解Vue2.0 事件派发与接收
下一篇:mybatis教程之增删改查_动力节点Java学院整理
相关文章

 发表评论

暂时没有评论,来抢沙发吧~