我前面使用的jdbc和jdbc的工具類集成的但是它們在少部分代碼的情況下會會簡單,但是以後如果項目較大jdbc的固定代碼會很難維護,如果使用框架會簡單很多,也標志著java學習正式進入到框架階段 ...
0x00前言
我前面使用的jdbc和jdbc的工具類集成的但是它們在少部分代碼的情況下會會簡單,但是以後如果項目較大jdbc的固定代碼會很難維護,如果使用框架會簡單很多,也標志著java學習正式進入到框架階段
0x01配置MyBaits
0x1創建模塊,導入坐標
在maven的配置文件中pom.xml中加入所需要的jar包
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
0x2配置資料庫連接文件
修改自己的資料庫地址和密碼;
<?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>
<!-- 別名-->
<typeAliases>
<package name="com.it.pojo"/>
</typeAliases>
<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/tese"/>
<property name="username" value="root"/>
<property name="password" value="zhonglin"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
0x3編寫sql映射文件
這個文件存在的意義是為了以後的開發,查詢那個表那個表的語句就寫一個這樣的映射表,方便管理和維護
<?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">
<mapper namespace="test">
<select id="findAll" resultType="com.test.domain.User">
select * from user;
</select>
</mapper>
在資料庫配置文件中要加入,文件名就是你的sql映射文件
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
0x02入門使用
0x1定義實體類
實體類的內容為你資料庫查詢的內容
public class User {
private String name;
private String password;
private String gender;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
", gender='" + gender + '\'' +
", id=" + id +
'}';
}
0x2基礎使用
-
載入核心配置文件
-
獲取sqlSession對象,執行SQL語句
-
釋放資源
public class demo1 { public static void main(String[] args) throws IOException { //1.載入mybatis配置文件,獲取SqlSessionFactory,sql語句工廠 InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");//輸出流獲取配置文件地址 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();//獲取bulid對象然後獲取Factory對象 SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream); SqlSession sqlSession = build.openSession(); List<User> users = sqlSession.selectList("test.findAll");//通過sql語句的映射文件去查詢 System.out.println(users); sqlSession.close(); } }
0x03代理開發
0x1配置映射文件和介面
定義sql映射文件相同名字的Mapper文件,且文件路徑要一致,就是要在同一個文件夾下麵
UserMapper文件:
public interface UserMapper {
List<User> findAll();
}
映射文件是在resources下麵的所以我們要給它新的一個文件夾他的命名方式是com/test而介面的命名方式是com.test這點是要註意的,隨後需要修改
0x2配置核心文件
<mappers>
<mapper resource="com/test/UserMapper.xml"/>
</mappers>
需要把在mybatis-config中的映射聲明改成新的文件路徑
0x3使用Mapper開發
- 還是先獲取配置文件,然後用sqlSession對象獲取userMapper.class文件,mapper就是UserMapper的代理對象了
- 用代理對象去實現UserMapper中的方法
public class demo2 {
public static void main(String[] args) throws IOException {
//1.載入mybatis配置文件,獲取SqlSessionFactory,sql語句工廠
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
SqlSession sqlSession = build.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> all = mapper.findAll();
System.out.println(all);
0x04MyBatis的增刪改查
0x1條件查詢
如果是對同一個表進行的操作就寫在一個介面裡面
public interface UserMapper {
List<User> findAll();
User selectbyID(int id);
}
在對於的xml文件裡面寫入對應的sql語句
<mapper namespace="com.test.domain.com.test.UserMapper">
<select id="findAll" resultType="com.test.domain.User">
select * from user;
</select>
<select id="selectbyID" resultType="com.test.domain.User">
select * from user where id =#{id}
</select>
</mapper>
還是用代理的方式進行開發和查詢
public class dmo3 {
public static void main(String[] args) throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
SqlSession sqlSession = build.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.selectbyID(5);
System.out.println(user);
sqlSession.close();
}
}
0x2resultMap標簽
<resultMap id="paswwrod" type="com.test.domain.User">
<result column="paswword" property="password"></result>
</resultMap>
這個標簽的作用可以用於當你的pojo(實體)對象的屬性和你的數據的表明對不上的時候可以做一個屬性的映射
- id=唯一表示符合
- type=pojo對象
- property:實體類的屬性名
0x3增刪改查
學習完這些基礎的以後剩下的就是sql代碼的問題了列如;模糊查詢應該怎麼做
1.在對應表的介面寫出方法
public interface UserMapper {
List<User> findlike(String name);
}
寫出對於的sql語句,模糊查詢select *(需要的屬性名) from user(對應的表名) where name like #{name}(條件)
<select id="findlike" resultMap="paswwrod">
select * from user where name like #{mame}
</select>
對應的刪除方法
public interface UserMapper {
void delById(int id );
}
<delete id="delById">
delete * from user where id=#{id}
</delete>
對應修改方法
void updateId(User user);
<update id="updateId">
UPDATE user set
name =#{name},
gender=#{gender},
password=#{password}
where id=#{id}
</update>
public static void main(String[] args) throws IOException {
User user = new User();
user.setGender("男");
user.setName("小小");
user.setPassword("123452");
user.setId(14);
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
SqlSession sqlSession = build.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateId(user);
對應修改方法
void add(User user);
<insert id="add">
insert into user (name,password,gender)
values (#{name},#{password},#{gender})
</insert>
public static void main(String[] args) throws IOException {
User user = new User();
user.setPassword("root");
user.setName("hellopw");
user.setGender("女");
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
SqlSession sqlSession = build.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.add(user);
sqlSession.close();
#0x05總結
其實現在的MyBatis還有一些負責當它和spring一起使用的時候會更簡單,需要記住的是sql語句的語法。