原生Dao開發方法需要程序員編寫Dao介面和Dao實現類
知識
07-15
目錄結構
User.xml 映射文件:
- <?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">
- <!-- namespace: 命名空間,做 sql隔離 -->
- <mapper namespace="test">
- <!--
- id: sql語句唯一標識
- parameterType: 指定傳入參數類型
- resultType: 返回結果集類型
- #{}:佔位符,起到佔位作用,如果傳入的是基本數據類型(String,Integer...),那麼#{}中的變數名稱可以隨意寫
- -->
- <select id="findUserById" parameterType="java.lang.Integer" resultType="com.jadan.po.User">
- select * from user where id = #{id}
- </select>
- <!--
- 如果返回結果為集合,可以調用selectList方法,這個方法返回的結果就是一個集合,所以映射文件中應該配置成集合泛型的類型
- ${}拼接符: 字元串原樣拼接,如果傳入的參數是基本類型,那麼${}中變數名稱必須是value
- 注意: 拼接符有sql注入的風險,所以慎重使用
- -->
- <select id="findUserByUsername" parameterType="java.lang.String" resultType="com.jadan.po.User">
- select * from user where username like "%${value}%"
- </select>
- <!--
- #{}: 如果傳入的是pojo類型,那麼#{}中的變數名稱必須是pojo中對象的屬性.屬性.屬性...
- 如果要返回資料庫自增主鍵: 可以使用select LAST_INSERT_ID()
- -->
- <insert id="insertUser" parameterType="com.jadan.po.User" >
- <!-- 執行select LAST_INSERT_ID() 資料庫函數,返回自增的主鍵
- keyProperty: 將返回的主鍵放入傳入參數的Id中保存
- order: 當前函數相對於insert語句的執行順序,在insert前執行是before,在insert後執行是AFTER
- resultType: id的類型,也就是keyProperties中屬性的類型
- -->
- <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
- select LAST_INSERT_ID()
- </selectKey>
- insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})
- </insert>
- <!-- 更新用戶 -->
- <update id="updateUserById" parameterType="com.jadan.po.User">
- update user set sex = #{sex} where id = #{id}
- </update>
- <!-- 刪除用戶 -->
- <delete id="delUserById" parameterType="java.lang.Integer">
- delete from user where id = #{id}
- </delete>
- </mapper>
SqlMapConfig.xml 核心配置文件:
- <?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>
- <!-- 和Spring整合後 environments配置將廢除 -->
- <environments default="development">
- <environment id="development">
- <!-- 使用jdbc事務管理 -->
- <transactionManager type="JDBC" />
- <!-- 資料庫連接池 -->
- <dataSource type="POOLED">
- <property name="driver" value="com.mysql.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/mybatis01?characterEncoding=utf-8"/>
- <property name="username" value="root"/>
- <property name="password" value="1234"/>
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <mapper resource="User.xml"/>
- </mappers>
- </configuration>
UserDao介面:
- package com.jadan.dao;
- import java.util.List;
- import com.jadan.po.User;
- public interface UserDao {
- // 通過id查用戶
- public User findUserById(Integer id);
- // 通過用戶名查詢用戶
- public List<User> findUserByUsername(String username);
- // 插入用戶數據
- public void insertUser(User user);
- // 更改數據
- public void updateUserById(User user);
- // 刪除數據
- public void delUserById(Integer id);
- }
UserDaoImpl 實現類:
- package com.jadan.dao;
- import java.util.List;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import com.jadan.po.User;
- public class UserDaoImpl implements UserDao {
- private SqlSessionFactory sqlSessionFactory;
- // 通過構造方法注入會話工廠
- public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
- this.sqlSessionFactory = sqlSessionFactory;
- }
- // 通過id查詢用戶
- public User findUserById(Integer id) {
- // sqlSession是線程不安全的,所以它的最佳使用範圍在方法體內
- SqlSession openSession = sqlSessionFactory.openSession();
- User user = openSession.selectOne("test.findUserById", 1);
- return user;
- }
- // 通過用戶名查詢用戶
- public List<User> findUserByUsername(String username) {
- SqlSession openSession = sqlSessionFactory.openSession();
- List<User> list = openSession.selectList("test.findUserByUsername", username);
- return list;
- }
- // 插入用戶數據
- public void insertUser(User user) {
- SqlSession openSession = sqlSessionFactory.openSession();
- openSession.insert("test.insertUser", user);
- // 記得提交事務
- openSession.commit();
- }
- // 更改用戶數據
- public void updateUserById(User user) {
- SqlSession openSession = sqlSessionFactory.openSession();
- openSession.update("test.updateUserById", user);
- openSession.commit();
- }
- // 刪除用戶
- public void delUserById(Integer id) {
- SqlSession openSession = sqlSessionFactory.openSession();
- openSession.delete("test.delUserById", id);
- openSession.commit();
- }
- }
UserDaoTest 測試類:
- package mybatis_day01;
- import java.io.InputStream;
- import java.util.Date;
- import java.util.List;
- import org.apache.ibatis.io.Resources;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
- import org.junit.Before;
- import org.junit.Test;
- import com.jadan.dao.UserDao;
- import com.jadan.dao.UserDaoImpl;
- import com.jadan.po.User;
- public class UserDaoTest {
- private SqlSessionFactory factory;
- // 作用: 在測試方法前執行這個初始化方法
- @Before
- public void setUp() throws Exception {
- // 核心配置文件名
- String resource = "SqlMapConfig.xml";
- // 通過流將核心配置文件讀取進來
- InputStream inputStream = Resources.getResourceAsStream(resource);
- // 通過核心配置文件輸入流來創建會話工廠
- factory = new SqlSessionFactoryBuilder().build(inputStream);
- }
- // 通過id查詢用戶
- @Test
- public void testFindUserById() throws Exception {
- UserDao userDao = new UserDaoImpl(factory);
- User user = userDao.findUserById(1);
- System.out.println(user);
- }
- // 通過用戶名模糊查詢用戶
- @Test
- public void testFindUserByUsername() throws Exception {
- UserDao userDao = new UserDaoImpl(factory);
- List<User> list = userDao.findUserByUsername("王");
- System.out.println(list);
- }
- // 測試: 插入用戶數據
- @Test
- public void testInsertUser() throws Exception {
- UserDao userDao = new UserDaoImpl(factory);
- User user = new User();
- user.setUsername("Jadan");
- user.setBirthday(new Date());
- user.setSex("男");
- user.setAddress("福建平潭");
- userDao.insertUser(user);
- }
- // 測試: 更新用戶數據
- @Test
- public void testUpdateUserById() throws Exception {
- UserDao userDao = new UserDaoImpl(factory);
- User user = new User();
- user.setId(41);
- user.setSex("女");
- userDao.updateUserById(user);
- }
- // 測試: 刪除用戶
- @Test
- public void testDelUserById() throws Exception {
- UserDao userDao = new UserDaoImpl(factory);
- userDao.delUserById(41);
- }
- }
※Cron表達式、定時任務
※js中setTimeOut()和setInterval()的使用——程序執行時間控制
TAG:程序員小新人學習 |