Mybatis实现数据的增删改查
Mybatis实现数据的增删改查
1、项目结构(使用maven创建项目)
2、App.java
1 package com.GetcharZp.MyBatisStudy; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.List; 6 7 import org.apache.ibatis.io.Resources; 8 import org.apache.ibatis.session.SqlSession; 9 import org.apache.ibatis.session.SqlSessionFactory; 10 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 11 12 import com.GetcharZp.MyBatisStudy.dao.TradeDao; 13 import com.GetcharZp.MyBatisStudy.model.Trade; 14 15 /** 16 * Hello world! 17 * 18 */ 19 public class App 20 { 21 public static void main( String[] args ) 22 { 23 InputStream config = null; 24 try { 25 config = Resources.getResourceAsStream("mybatis-config.xml"); 26 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(config); 27 SqlSession session = factory.openSession(); 28 TradeDao tradeDao = session.getMapper(TradeDao.class); 29 30 31 32 /*删除*/ 33 tradeDao.deleteTrade(3); 34 session.commit(); // 删除要提交 35 36 /*查询*/ 37 List<Trade> tradeList = tradeDao.selectTrade(); 38 System.out.println(tradeList); 39 System.out.println(tradeList.size()); 40 Trade trade = tradeDao.findById(2); 41 System.out.println(trade); // 日志里面有输出,但结果没有 42 43 /*插入*/ 44 Trade InsertTrade = new Trade(); 45 InsertTrade.setTradeId(4); 46 InsertTrade.setTradeName("毛笔"); 47 InsertTrade.setTradePrice(5.6); 48 tradeDao.insertTrade(InsertTrade); 49 session.commit(); 50 51 /*更新*/ 52 Trade updateTrade1 = new Trade(); 53 updateTrade1.setTradeName("watch"); 54 updateTrade1.setTradePrice(520.1314); 55 tradeDao.updateTrade(3); 56 session.commit(); 57 58 session.close(); 59 60 } catch (IOException e) { 61 e.printStackTrace(); 62 } finally { 63 if (config != null) 64 try { 65 config.close(); 66 } catch (IOException e) { 67 e.printStackTrace(); 68 } 69 } 70 } 71 }
3、TradeDao
1 package com.GetcharZp.MyBatisStudy.dao; 2 3 import java.util.List; 4 5 import com.GetcharZp.MyBatisStudy.model.Trade; 6 7 public interface TradeDao { // interface 接口 8 9 public List<Trade> selectTrade(); 10 public void deleteTrade(int tradeId); 11 public void updateTrade(int tradeId); 12 public void insertTrade(Trade newTrade); 13 public Trade findById(int tradeId); 14 }
4、Trade
1 package com.GetcharZp.MyBatisStudy.model; 2 3 public class Trade { 4 5 private Integer tradeId; 6 private String tradeName; 7 private Double tradePrice; 8 public Integer getTradeId() { 9 return tradeId; 10 } 11 public void setTradeId(Integer tradeId) { 12 this.tradeId = tradeId; 13 } 14 public String getTradeName() { 15 return tradeName; 16 } 17 public void setTradeName(String tradeName) { 18 this.tradeName = tradeName; 19 } 20 public Double getTradePrice() { 21 return tradePrice; 22 } 23 public void setTradePrice(Double tradePrice) { 24 this.tradePrice = tradePrice; 25 } 26 @Override 27 public String toString() { 28 return "Trade [tradeId=" + tradeId + ", tradeName=" + tradeName + ", tradePrice=" + tradePrice + "]"; 29 } 30 }
5、TradeMapper
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 7 <mapper namespace="com.GetcharZp.MyBatisStudy.dao.TradeDao"> 8 <select id="selectTrade" resultType="com.GetcharZp.MyBatisStudy.model.Trade"> 9 select * from trade 10 </select> 11 <select id="findById" resultType="com.GetcharZp.MyBatisStudy.model.Trade"> 12 select * from trade where trade_id=#{tradeId} 13 </select> 14 <delete id="deleteTrade"> 15 delete from trade where trade_id = #{tradeId} 16 </delete> 17 <insert id="insertTrade"> 18 insert into trade (trade_id, trade_name, trade_price) 19 values(#{tradeId}, #{tradeName}, #{tradePrice}) 20 </insert> 21 <update id="updateTrade"> 22 update trade set 23 trade_name = #{tradeName}, 24 trade_price = #{tradePrice} 25 where 26 trade_id = #{tradeId} 27 </update> 28 </mapper>
6、mybatis-config.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> <settings> <!-- <setting name="logImpl" value="STDOUT_LOGGING" /> --> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <typeAliases> <package name="com.GetcharZp.MyBatisStudy.model"/> </typeAliases> <environments default="dev"> <environment id="dev"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/suse"/> <property name="username" value="root"/> <property name="password" value="1234"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mappers/TradeMapper.xml"/> </mappers> </configuration>
7、pom.xml:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId>com.GetcharZp.MyBatisStudy</groupId> 6 <artifactId>Mybatis</artifactId> 7 <version>1.0-SNAPSHOT</version> 8 <packaging>jar</packaging> 9 10 <name>Mybatis</name> 11 <url>http://maven.apache.org</url> 12 13 <properties> 14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 15 </properties> 16 17 <dependencies> 18 <dependency> 19 <groupId>junit</groupId> 20 <artifactId>junit</artifactId> 21 <version>3.8.1</version> 22 <scope>test</scope> 23 </dependency> 24 <dependency> 25 <groupId>org.mybatis</groupId> 26 <artifactId>mybatis</artifactId> 27 <version>3.4.1</version> 28 </dependency> 29 30 <dependency> 31 <groupId>mysql</groupId> 32 <artifactId>mysql-connector-java</artifactId> 33 <version>5.1.38</version> 34 </dependency> 35 </dependencies> 36 </project>