标签:doctype vat 功能 xsd for action xmlns ltm efault
目的:使用Mybatis对数据库中的数据进行简单的操作。例如:增、删、改、查、
前言:同样是使用idea创建一个普通的maven工程(如何创建一个普通的Maven工程可以参考入门第一天的详解)。
数据库中的表仍然使用第一天的数据库表(新建表和添加数据的sql语句见第一天的分析)。
代码来了。。。。。。。。。。。。。。
首先看一下所有的文件的路径和结构
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>zh.test.mybatis</groupId> 8 <artifactId>zh_mybatis02</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <dependencies> 11 <!-- mybatis坐标 --> 12 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> 13 <dependency> 14 <groupId>org.mybatis</groupId> 15 <artifactId>mybatis</artifactId> 16 <version>3.4.5</version> 17 </dependency> 18 <!--mysql驱动包--> 19 <dependency> 20 <groupId>mysql</groupId> 21 <artifactId>mysql-connector-java</artifactId> 22 <version>5.1.6</version> 23 </dependency> 24 <!--单元测试--> 25 <dependency> 26 <groupId>junit</groupId> 27 <artifactId>junit</artifactId> 28 <version>4.10</version> 29 <scope>test</scope> 30 </dependency> 31 <!-- https://mvnrepository.com/artifact/log4j/log4j --> 32 <!--日志--> 33 <dependency> 34 <groupId>log4j</groupId> 35 <artifactId>log4j</artifactId> 36 <version>1.2.17</version> 37 </dependency> 38 </dependencies> 39 40 </project>
1 package zh.test.domain; 2 3 import java.io.Serializable; 4 import java.util.Date; 5 6 /* 7 编写一个User的实体类,并且实现Serializable接口,目的只是表示一个类的对象可以被序列化。 8 什么是Serializable接口? 9 一个对象序列化的接口,一个类只有实现了Serializable接口,他的对象才能被序列化 10 */ 11 public class NBAPlaers implements Serializable { 12 private Integer id; 13 private String username; 14 private Date birthday; 15 private String sex; 16 private String address; 17 18 @Override 19 public String toString() { 20 return "NBAPlaers{" + 21 "id=" + id + 22 ", username=‘" + username + ‘\‘‘ + 23 ", birthday=" + birthday + 24 ", sex=‘" + sex + ‘\‘‘ + 25 ", address=‘" + address + ‘\‘‘ + 26 ‘}‘; 27 } 28 29 public Integer getId() { 30 return id; 31 } 32 33 public void setId(Integer id) { 34 this.id = id; 35 } 36 37 public String getUsername() { 38 return username; 39 } 40 41 public void setUsername(String username) { 42 this.username = username; 43 } 44 45 public Date getBirthday() { 46 return birthday; 47 } 48 49 public void setBirthday(Date birthday) { 50 this.birthday = birthday; 51 } 52 53 public String getSex() { 54 return sex; 55 } 56 57 public void setSex(String sex) { 58 this.sex = sex; 59 } 60 61 public String getAddress() { 62 return address; 63 } 64 65 public void setAddress(String address) { 66 this.address = address; 67 } 68 }
新建一个UserMapper的接口,里面主要是要对数据库进行的哪些操作。如下:进行了多种操作
1 package zh.test.mapper; 2 3 import zh.test.domain.NBAPlaers; 4 5 import java.util.List; 6 7 /* 8 用户映射接口 9 */ 10 public interface UserMapper { 11 //查询所有的数据 12 public List<NBAPlaers> findAll(); 13 //根据条件查询 14 public NBAPlaers findById(int id); 15 //插入数据 16 public int insert(NBAPlaers nbaPlaers); 17 //修改数据 18 public int update(NBAPlaers nbaPlaers); 19 //删除一条数据 20 public int delete(int id); 21 //模糊查询 22 public List<NBAPlaers> findBylike(NBAPlaers name); 23 //聚合函数查询 24 public int findcount(); 25 }
1 driver=com.mysql.jdbc.Driver 2 url=jdbc:mysql://127.0.0.1:3306/javaDemo?characterEncoding=utf8 3 username=root 4 password=root
Usermapper.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!--加载jdbc的属性配置文件--> 7 <properties resource="jdbc.properties"/> 8 <!--配置实体类的别名--> 9 <typeAliases> 10 <!-- 11 type:实体类的全路径 12 alias:取的一个别名,不区分大小写,在配置文件调用这个实体类的时候就可以使用别名。 13 --> 14 <typeAlias type="zh.test.domain.NBAPlaers" alias="nbaers"/> 15 <!--如果有多个实体类可以写为:包名。别名则就是【类名】--> 16 <!--<package name="zh.test.domain"/>--> 17 </typeAliases> 18 <!--配置多个环境--> 19 <environments default="mysql"> 20 <!--配置环境--> 21 <environment id="mysql"> 22 <!--配置事物管理,使用本地的事物策略--> 23 <transactionManager type="JDBC"></transactionManager> 24 <!--是否要是要连接池 POOLED UNPOOLED--> 25 <dataSource type="POOLED"> 26 <property name="driver" value="${driver}"/> 27 <property name="url" value="${url}"/> 28 <property name="username" value="${username}"/> 29 <property name="password" value="${password}"/> 30 <!--<property name="url" value="jdbc:mysql:///javaDemo"/>--> 31 </dataSource> 32 </environment> 33 </environments> 34 <!--引入映射的配置文件--> 35 <mappers> 36 <mapper resource="mappers/Usermapper.xml"></mapper> 37 </mappers> 38 </configuration>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!--约束 dtd约束--> 3 <!DOCTYPE mapper 4 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 5 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 6 <!-- 7 namespace:用来区分mapper接口的,一般写mapper的全路径 8 --> 9 10 <mapper namespace="zh.test.mapper.UserMapper"> 11 <!-- 12 id属性:表示接口中方法的名称 13 resultType:方法返回值类型,全限定路径(包名+类名) 14 --> 15 <select id="findAll" resultType="zh.test.domain.NBAPlaers"> 16 /* 17 编写对应的sql语句 18 */ 19 select * from NBAPlaers; 20 </select> 21 <!--根据条件查询--> 22 <!-- 23 id:findById 方法的名称 24 parameterType:方法参数的类型 25 resultType:方法返回值的类型 26 27 ? 不能使用?作为占位符,使用#{} 或${}来编写,推荐使用#{} 28 区别:1、#{}. 29 2、${} 30 以上两种,当方法的参数为【引用类型】的时候,两种都可以使用,没有区别 31 当方法的参数为【普通数据类型(int double )】推荐使用#{此处的方法的名字可以随意命名},但是${此处的方法的名字的参数必须与写value} 32 --> 33 <select id="findById" parameterType="java.lang.Integer" resultType="zh.test.domain.NBAPlaers"> 34 select * from NBAPlaers where id=#{id} 35 </select> 36 <!--新增--> 37 <!-- 38 #{OGNL表达式}:对象导航语言,表达式语言。语法和el表达式相似 39 el表达式只能在jsp页面上使用。OGNL表达式既可以在页面上又可以在配置文件中使用。 40 --> 41 <insert id="insert" parameterType="zh.test.domain.NBAPlaers"> 42 -- keyProperty="封装数据使用的属性" 43 <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> 44 select last_insert_id(); 45 </selectKey> 46 insert into NBAPlaers (username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address}); 47 </insert> 48 <!--修改数据--> 49 <update id="update" parameterType="zh.test.domain.NBAPlaers"> 50 update NBAPlaers set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}; 51 </update> 52 <!--删除一条数据--> 53 <delete id="delete" parameterType="java.lang.Integer"> 54 delete from NBAPlaers where id=#{id}; 55 </delete> 56 <!--模糊查询--> 57 <select id="findBylike" parameterType="java.lang.String" resultType="zh.test.domain.NBAPlaers"> 58 select * from NBAPlaers where username like #{username} 59 </select> 60 <!--聚合函数查询--> 61 <select id="findcount" resultType="java.lang.Integer"> 62 select COUNT(*) from NBAPlaers; 63 </select> 64 <!-- 65 resultMap:用来进行数据的封装 66 id="唯一的名称,用来被引用" 67 type="进行数据封装的数据的类型" 68 --> 69 <resultMap id="nbaMapper" type="zh.test.domain.NBAPlaers"> 70 <!-- 71 property="JavaBean中的属性" 72 column="表中的字段" 73 --> 74 <result property="id" column="id"></result> 75 <result property="username" column="_username"></result> 76 <result property="sex" column="_sex"></result> 77 <result property="address" column="_address"></result> 78 </resultMap> 79 </mapper>
1 package test; 2 3 import org.apache.ibatis.io.Resources; 4 import org.apache.ibatis.session.SqlSession; 5 import org.apache.ibatis.session.SqlSessionFactory; 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 7 import org.junit.Test; 8 import zh.test.domain.NBAPlaers; 9 import zh.test.mapper.UserMapper; 10 11 import java.io.IOException; 12 import java.io.InputStream; 13 import java.net.SocketTimeoutException; 14 import java.util.Date; 15 import java.util.List; 16 17 public class Demo { 18 19 @Test 20 public void findUserAll() throws IOException { 21 //加载主配置文件 22 InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml"); 23 SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); 24 SqlSession sqlSession = build.openSession(); 25 //获取到代理对象,mybatis框架生成代理对象 26 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 27 //mapper指向就是代理对象 28 List<NBAPlaers> allPlayers = mapper.findAll(); 29 for(NBAPlaers user:allPlayers) 30 { 31 System.out.println(user); 32 } 33 sqlSession.close(); 34 resourceAsStream.close(); 35 } 36 37 @Test 38 public void findById() throws IOException { 39 40 InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml"); 41 SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); 42 SqlSession sqlSession = build.openSession(); 43 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 44 NBAPlaers nbaPlaer = mapper.findById(2); 45 System.out.println(nbaPlaer); 46 sqlSession.close(); 47 resourceAsStream.close(); 48 // 这里代码看不懂可以查看mybatis01的代码 49 } 50 51 // 插入数据 52 @Test 53 public void insert() throws IOException { 54 InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml"); 55 SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); 56 SqlSession sqlSession = build.openSession(); 57 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 58 NBAPlaers nba=new NBAPlaers(); 59 nba.setAddress("洛杉矶"); 60 nba.setBirthday(new Date()); 61 nba.setSex("男"); 62 nba.setUsername("裤子马"); 63 int insert = mapper.insert(nba); 64 System.out.println(insert); 65 //到这运行控制台会输入执行一行数据成功,但是但是但是这条数据并没有插入到数据库中。没有commit commit commit!!!! 66 //Setting autocommit to false on JDBC Connection 67 //要手动提交事物 68 sqlSession.commit(); 69 70 //其中sqlSession.rollback();为回滚 71 //获取刚才插入数据的ID值,注意:需要在UserMapper.xml中配置文件 72 Integer id = nba.getId(); 73 System.out.println("id:"+id); 74 sqlSession.close(); 75 resourceAsStream.close(); 76 77 } 78 // 修改数据 79 @Test 80 public void update() throws IOException { 81 InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml"); 82 SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); 83 SqlSession sqlSession = build.openSession(); 84 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 85 NBAPlaers nba=new NBAPlaers(); 86 nba.setUsername("隆多"); 87 nba.setSex("男"); 88 nba.setBirthday(new Date()); 89 nba.setAddress("洛杉矶"); 90 nba.setId(7); 91 int update = mapper.update(nba); 92 //提交事物 93 sqlSession.commit(); 94 System.out.println(update); 95 sqlSession.close(); 96 resourceAsStream.close(); 97 } 98 // 删除数据 99 @Test 100 public void delte() throws IOException { 101 InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml"); 102 SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); 103 SqlSession sqlSession = build.openSession(); 104 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 105 NBAPlaers nba=new NBAPlaers(); 106 int update = mapper.delete(7); 107 //提交事物 108 sqlSession.commit(); 109 System.out.println(update); 110 sqlSession.close(); 111 resourceAsStream.close(); 112 } 113 //模糊查询 114 @Test 115 public void findBilike() throws IOException { 116 InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml"); 117 SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); 118 SqlSession sqlSession = build.openSession(); 119 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 120 NBAPlaers nba=new NBAPlaers(); 121 nba.setUsername("%L%"); 122 List<NBAPlaers> bylike = mapper.findBylike(nba); 123 for(NBAPlaers nba1:bylike) 124 { 125 System.out.println(nba1); 126 } 127 sqlSession.close(); 128 resourceAsStream.close(); 129 } 130 //聚合函数的查询数据量 131 @Test 132 public void findcount() throws IOException { 133 InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml"); 134 SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); 135 SqlSession sqlSession = build.openSession(); 136 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 137 int count = mapper.findcount(); 138 System.out.println(count); 139 sqlSession.close(); 140 resourceAsStream.close(); 141 } 142 }
分享之路,欢迎交流
标签:doctype vat 功能 xsd for action xmlns ltm efault
原文地址:https://www.cnblogs.com/LBJLAKERS/p/11423025.html