Springboot from getting started to getting started (nine ORM mybatis)

wwz_ henu 2022-02-13 05:19:44 阅读数:480

springboot getting started getting started

Reference resources :https://mybatis.org/mybatis-3/zh/index.html

One . increase dependency

 <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>

Two . Add profile

stay resources Add... To the folder mybatis-config.xml To store MyBatis To configure

<?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>
<properties resource="properties.yml"/>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>

among ,properties.yml Also located in resources In the folder It stores some database configuration information , Other configurations can also be stored

driver: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test
username: root
password: 123456

UserMapper.xml Also located in resources In the folder , Store specific query information ,id Call ID ,resultType Is the return data type

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="getUser" resultType="com.example.demo.entity.UserEntity">
select * from user
</select>
</mapper>

3、 ... and . Create a route and access

 @RequestMapping("/users")
public List<UserEntity> users() throws IOException {
// mybatis-config.xml For the file created in step 2
String resource = "mybatis-config.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sessionFactory.openSession(true);
// getUser by UserMapper.xml Query defined in id
return sqlSession.selectList("getUser");
}

Four . Queries with parameters

edit UserMapper.xml, Add the following query

<select id="getOneUser" resultType="com.example.demo.entity.User">
select * from user where id = #{id}
</select>

Create a route and access

@RequestMapping("/user")
public UserEntityuser(int id) throws IOException {
String resource = "mybatis-config.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sessionFactory.openSession(true);
return sqlSession.selectOne("getOneUser",id);
}

5、 ... and . In the above way, when calling , Not clear enough , You can call... In another way

com.example.demo Create mapper package, And create UserMapper
package com.example.demo.mapper;
import com.example.demo.entity.UserEntity;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserMapper {
List<UserEntity> getUser();
UserEntity getOneUser(@Param("id") int id);
}

Create a route and access

 @RequestMapping("/users1")
public List<UserEntity> users1() throws IOException {
String resource = "mybatis-config.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.getUser();
}
@RequestMapping("/user1")
public UserEntityuser1(int id) throws IOException {
String resource = "mybatis-config.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.getOneUser(id);
}

6、 ... and . Except above mapper How to introduce , There are other ways to introduce

https://www.cnblogs.com/my-ordinary/p/13503085.html

copyright:author[wwz_ henu],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130519421325.html