SQL:
CREATE TABLE `tb_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(20) DEFAULT NULL, `password` varchar(20) DEFAULT NULL, `gender` char(1) DEFAULT NULL, `addr` varchar(30) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
INSERT INTO tb_user VALUES (1,'zhangsan','123','男','北京'); INSERT INTO tb_user VALUES (2,'李四','111','女','上海'); INSERT INTO tb_user VALUES (3,'王五','122','男','河北');
创建Maven项目:
项目结构:
pom.xml:
<dependencies> <!-- mybatis的依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <!-- mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency> <!-- junit单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
java下创建pojo包,pojo包下创建User.java:
public class User { private Integer id; private String username; private String password; private String gender; private String addr; //getter&setter 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 getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } // toString @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", gender='" + gender + '\'' + ", addr='" + addr + '\'' + '}'; } }
resource下新建mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://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.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///mybatis?useSSL=false"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!-- 加载SQL映射文件路径--> <mappers> <mapper resource="UserMapper.xml"/> </mappers> </configuration>
新建mapper包,包下新建UserMapper接口。
import pojo.User; import java.util.List; public interface UserMapper { List<User> selectAll(); }
resource下新建UserMapper.xml映射.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="testmapper"> <!--名称空间--> <select id="selectAll" resultType="pojo.User"><!--id:唯一标识 resultType:返回结果的类型--> select * from tb_user; </select> </mapper>
java下创建测试类test.java:
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 pojo.User; import java.io.IOException; import java.io.InputStream; import java.util.List; public class test { public static void main(String[] args) throws IOException { //1.加载mybatis的核心配置文件,获取sqlSessionFactory String resource = "mybatis-config.xml";//定义配置文件路径 InputStream inputStream = Resources.getResourceAsStream(resource);//输入流进行资源加载,传入字符串返回输入流 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //2.获取sqlSession对象,用它来执行sql SqlSession sqlSession = sqlSessionFactory.openSession(); //3.执行sql,自动封装结果集 List<User> users = sqlSession.selectList("testmapper.selectAll"); System.out.println(users); //4.释放资源 sqlSession.close(); } }
在test.java类中启动运行:
运行成功!
标签:java,String,id,查询,MyBatis,import,password,public,SpringBoot From: https://www.cnblogs.com/lysboke/p/17524727.html