#MyBatis增删改查 #MyBatis的CRUD操作 #自己编写dao实现类 #FDDLC

    科技2024-01-31  106

    编码流程:

     

    补张项目结构图:

     

    0:新建Maven工程,导入Maven依赖:mysql-connector-java、mybatis、junit。

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>P068_MyBatis_CRUD_imlements_dao</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>

     

    1.1、准备好数据库,新建account表(id,name,money),填充若干条记录。

    database:test        port:3306        table:account

     

    1.2、编写account表对应的实体类Account。

    package cn.liuxingchang.domain; import java.io.Serializable; public class Account implements Serializable { private Integer id; private String name; private Double money; @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } }

     

    2.1、编写AccountDao接口。

    package cn.liuxingchang.dao; import cn.liuxingchang.domain.Account; import java.util.List; public interface AccountDao { List<Account> findAll(); Account findOne(Integer id); List<Account> findByName(String name); Integer count(); void insert(Account account); void update(Account account); void delete(Integer id); }

     

    2.2、编写AccountMapper.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"> <mapper namespace="cn.liuxingchang.dao.AccountDao"> <select id="findAll" resultType="cn.liuxingchang.domain.Account"> select * from account </select> <select id="findOne" resultType="cn.liuxingchang.domain.Account" parameterType="int"> select * from account where id = #{id} </select> <select id="findByName" parameterType="String" resultType="cn.liuxingchang.domain.Account"> select * from account where name like #{name} </select> <select id="count" resultType="int"> select count(*) from account </select> <insert id="insert" parameterType="cn.liuxingchang.domain.Account"> insert into account (name, money) values (#{name}, #{money}) </insert> <update id="update" parameterType="cn.liuxingchang.domain.Account"> update account set name = #{name}, money = #{money} </update> <delete id="delete" parameterType="int"> delete from account where id = #{id} </delete> </mapper>

    说明:

    1、<mapper>标签的namespace属性是必需的!

    2、我们知道,如果不写dao实现类,而是使用MyBatis的代理dao的话,要求很严格:

    AccountDao接口的全路径为【cn.liuxingchang.dao.AccountDao】,方法名为【findAll】,

    那么<mapper>的namespace必须为【cn.liuxingchang.dao.AccountDao】,id必须为【findAll】!

    那么,自己编写dao实现类呢?

    当然就没上面这两个必须啦!即namespace和id可以自定义!

     

    2.3、编写AccountDao接口的实现类AccountDaoImpl。

    package cn.liuxingchang.dao.impl; import cn.liuxingchang.dao.AccountDao; import cn.liuxingchang.domain.Account; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import java.util.List; public class AccountDaoImpl implements AccountDao { private SqlSessionFactory factory; public AccountDaoImpl(SqlSessionFactory factory) { this.factory = factory; } public List<Account> findAll() { SqlSession session = factory.openSession(); List<Account> accounts = session.selectList("cn.liuxingchang.dao.AccountDao.findAll"); session.close(); return accounts; } public Account findOne(Integer id) { SqlSession session = factory.openSession(); Account account = session.selectOne("cn.liuxingchang.dao.AccountDao.findOne", id); session.close(); return account; } public List<Account> findByName(String name) { SqlSession session = factory.openSession(); List<Account> accounts = session.selectList("cn.liuxingchang.dao.AccountDao.findByName", name); session.close(); return accounts; } public Integer count() { SqlSession session = factory.openSession(); Integer count = session.selectOne("cn.liuxingchang.dao.AccountDao.count"); session.close(); return count; } public void insert(Account account) { SqlSession session = factory.openSession(true); //true:自动提交事务 session.insert("cn.liuxingchang.dao.AccountDao.insert", account); session.close(); } public void update(Account account) { SqlSession session = factory.openSession(true); session.update("cn.liuxingchang.dao.AccountDao.update", account); session.close(); } public void delete(Integer id) { SqlSession session = factory.openSession(true); //true:自动提交事务 session.delete("cn.liuxingchang.dao.AccountDao.delete", id); session.close(); } }

     

    3、编写MyBatis配置文件mybatis.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> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/Test?characterEncoding=UTF8&amp;serverTimezone=Asia/Shanghai"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/AccountMapper.xml" /> </mappers> </configuration>

     

    4、编写测试类Test。

    import cn.liuxingchang.dao.AccountDao; import cn.liuxingchang.dao.impl.AccountDaoImpl; import cn.liuxingchang.domain.Account; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.After; import org.junit.Before; import java.io.InputStream; import java.util.List; public class Test { private InputStream in; private SqlSessionFactory factory; private AccountDao dao; @Before public void before() throws Exception { in = Resources.getResourceAsStream("mybatis.xml"); factory = new SqlSessionFactoryBuilder().build(in); dao = new AccountDaoImpl(factory); } @After public void after() throws Exception { in.close(); } @org.junit.Test public void findAllTest() { List<Account> accounts = dao.findAll(); for(Account account: accounts) { System.out.println(account); } } @org.junit.Test public void findOneTest() { Account account = dao.findOne(1); System.out.println(account); } @org.junit.Test public void findByNameTest() { List<Account> accounts = dao.findByName("%a%"); for(Account account: accounts) { System.out.println(account); } } @org.junit.Test public void countTest() { Integer count = dao.count(); System.out.println(count); } @org.junit.Test public void insertTest() { Account account = new Account(); account.setName("MyBatis"); account.setMoney(666.0); dao.insert(account); } @org.junit.Test public void deleteTest() { dao.delete(5); } }

     

    Processed: 0.009, SQL: 8