生活随笔
收集整理的這篇文章主要介紹了
mybatis --入门 单表增删改查-curd
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 1. mybatis 環境搭建
- 2. 實體類映射文件配置(寫sql)
- 3. mybatis核心配置文件 (環境配置)
- 4. 測試
mybatis document
https://mybatis.org/mybatis-3/zh/
mybatis in github
https://github.com/mybatis/mybatis-3
1. mybatis 環境搭建
這里使用maven項目構建
<dependencies><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>8.0.20
</version></dependency><dependency><groupId>org.mybatis
</groupId><artifactId>mybatis
</artifactId><version>3.5.5
</version></dependency> </dependencies>
2. 實體類映射文件配置(寫sql)
package cn
.bitqian
.entity
;
public class User {private Integer userId
;private String userName
;private String userPassword
;public User() {}public User(Integer userId
, String userName
, String userPassword
) {this.userId
= userId
;this.userName
= userName
;this.userPassword
= userPassword
;}
}
- user-mapper.xml(注意命名空間,標簽 id,占位符)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="userMapper"><select id="selectAll" resultType="cn.bitqian.entity.User">select * from users1
</select><insert id="insertUser" parameterType="cn.bitqian.entity.User">insert into users1 values (#{userId}, #{userName}, #{userPassword})
</insert><update id="updateUser" parameterType="cn.bitqian.entity.User">update users1 set username = #{userName}, userpassword = #{userPassword}where userid = #{userId}
</update><delete id="deleteUser" parameterType="java.lang.Integer">delete from users1 where userid = #{userId}
</delete>
</mapper>
3. mybatis核心配置文件 (環境配置)
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis_study?serverTimezone=GMT
username=root
password=123456
- mybatis-config 配置 (注意頭文件的引入)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><properties resource="jdbc.properties"></properties><environments default="development"><environment id="development"><transactionManager type="JDBC"></transactionManager><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="cn.bitqian.mapper/user-mapper.xml"/></mappers>
</configuration>
- 包名就叫cn.bitqian.mapper, 沒有分層
4. 測試
- 由于初學,模板化的代碼我寫了四遍… 其中包含了單表curd
package DemoTest
;import cn
.bitqian
.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 org
.junit
.Test
;import java
.io
.IOException
;
import java
.io
.InputStream
;
import java
.util
.List
;
public class TestMybatisCurd {@Testpublic void test1() {SqlSession sqlSession
= null
;try {InputStream inputStream
= Resources
.getResourceAsStream("mybatis-config.xml");SqlSessionFactory sqlSessionFactory
= new SqlSessionFactoryBuilder().build(inputStream
);sqlSession
= sqlSessionFactory
.openSession();List
<User> userList
= sqlSession
.selectList("userMapper.selectAll");System
.out
.println(userList
);} catch (IOException e
) {e
.printStackTrace();} finally {if (sqlSession
!= null
)sqlSession
.close();}}@Testpublic void test2() {SqlSession sqlSession
= null
;try {InputStream inputStream
= Resources
.getResourceAsStream("mybatis-config.xml");SqlSessionFactory sqlSessionFactory
= new SqlSessionFactoryBuilder().build(inputStream
);sqlSession
= sqlSessionFactory
.openSession();User user
= new User(null
, "taylor swift", "gorgeous");int count
= sqlSession
.insert("userMapper.insertUser", user
);sqlSession
.commit();System
.out
.println("count = " + count
);} catch (IOException e
) {e
.printStackTrace();} finally {if (sqlSession
!= null
)sqlSession
.close();}}@Testpublic void test3() {SqlSession sqlSession
= null
;try {InputStream resourceAsStream
= Resources
.getResourceAsStream("mybatis-config.xml");SqlSessionFactory sqlSessionFactory
= new SqlSessionFactoryBuilder().build(resourceAsStream
);sqlSession
= sqlSessionFactory
.openSession();User user
= new User(1, "rose", "love jack");int count
= sqlSession
.update("userMapper.updateUser", user
);sqlSession
.commit();System
.out
.println(count
);} catch (IOException e
) {e
.printStackTrace();} finally {if (sqlSession
!= null
)sqlSession
.close();}}@Testpublic void tes4() {SqlSession sqlSession
= null
;try {InputStream resourceAsStream
= Resources
.getResourceAsStream("mybatis-config.xml");SqlSessionFactory sqlSessionFactory
= new SqlSessionFactoryBuilder().build(resourceAsStream
);sqlSession
= sqlSessionFactory
.openSession();sqlSession
.delete("userMapper.deleteUser", 1);sqlSession
.commit();} catch (IOException e
) {e
.printStackTrace();} finally {if (sqlSession
!= null
)sqlSession
.close();}}}
總結
以上是生活随笔為你收集整理的mybatis --入门 单表增删改查-curd的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。