标签:row exception 映射 log4j community tst develop app 模板
1.创建工程
这里为了方便直接用一个模板
创建完毕
2.引入mybatis所需jar包
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency> //测试相关 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> //日志相关 <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency>
3.创建一个实体类对应数据字段,并在数据库中创建对应的数据表
public class User implements Serializable { private static final long serialVersionUID = 8104961490512068314L; private int id; private String name; private String dept; private String phone; private String website; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getWebsite() { return website; } public void setWebsite(String website) { this.website = website; } @Override public String toString() { return "User{" + "id=" + id + ", name=‘" + name + ‘\‘‘ + ", dept=‘" + dept + ‘\‘‘ + ", phone=‘" + phone + ‘\‘‘ + ", website=‘" + website + ‘\‘‘ + ‘}‘; } }
4.添加mysql配置文件,mybatis配置文件
mybatis-config.xml
1 <configuration> 2 <properties resource="mysql.properties"></properties> 3 4 5 <typeAliases> 6 <!-- 别名方式1,一个一个的配置 type中放置的是类的全路径,alias中放置的是类别名 7 <typeAliase type="com.mybatis.demo.Entity.User" alias="UserBean"/> --> 8 <!-- 别名方式2,自动扫描,将JAVA类的类名作为类的类别名 --> 9 <!--<package name="com.mybatis.demo.Entity"/>--> 10 <typeAlias alias="User" type="com.mybatis.demo.Entity.User"/> 11 </typeAliases> 12 13 <!-- 配置mybatis运行环境 --> 14 <environments default="development"> 15 <environment id="development"> 16 <!-- type="JDBC" 代表使用JDBC的提交和回滚来管理事务 --> 17 <transactionManager type="JDBC"/> 18 19 <!-- mybatis提供了3种数据源类型,分别是:POOLED,UNPOOLED,JNDI --> 20 <!-- POOLED 表示支持JDBC数据源连接池 --> 21 <!-- UNPOOLED 表示不支持数据源连接池 --> 22 <!-- JNDI 表示支持外部数据源连接池 --> 23 <dataSource type="POOLED"> 24 <property name="driver" value="${jdbc.driver}"/> 25 <property name="url" value="${jdbc.databaseurl}"/> 26 <property name="username" value="${jdbc.username}"/> 27 <property name="password" value="${jdbc.password}"/> 28 </dataSource> 29 </environment> 30 </environments> 31 32 33 <mappers> 34 <!-- 映射文件方式1,一个一个的配置--> 35 <mapper resource="mapper/UserMapper.xml"/> 36 <!-- 映射文件方式2,自动扫描包内的Mapper接口与配置文件 --> 37 <!--<package name="com.mybatis.demo"/>--> 38 </mappers> 39 40 </configuration>
mysql.properties:
1 jdbc.databaseurl=jdbc:mysql:///javaweb_community?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false 2 jdbc.driver=com.mysql.cj.jdbc.Driver 3 jdbc.username=root 4 jdbc.password= 5 jdbc.maxActive=100 6 jdbc.maxWait=5000 7 jdbc.filters=wall,stat
ps: 这里mysql的jar包版本在6.0以上的和以前版本有一点差异。
5.添加mapper类接口以及对应mapper.xml
1 package com.mybatis.demo.mapper; 2 3 4 import com.mybatis.demo.Entity.User; 5 6 public interface UserMapper { 7 8 User GetUserByID(Integer id) throws Exception;
10 11 }
1 <mapper namespace="com.mybatis.demo.mapper.UserMapper"> 2 <!-- 自定义返回结果集 --> 3 <resultMap id="userMap" type="User"> 4 <id property="id" column="id" javaType="java.lang.Integer"></id> 5 <result property="username" column="username" javaType="java.lang.String"></result> 6 <result property="password" column="password" javaType="java.lang.String"></result> 7 <result property="account" column="account" javaType="java.lang.Double"></result> 8 </resultMap> 9 10 11 <select id="GetUserByID" parameterType="int" resultMap="userMap"> 12 select * from `user` where id = #{id} 13 </select> 14 </mapper>
6.在配置文件中注册该xml
7.编写单元测试,通过sqlSession 操作数据库
1 public class AppTest 2 { 3 /** 4 * Rigorous Test :-) 5 */ 6 @Test 7 public void shouldAnswerWithTrue() 8 { 9 10 try { 11 //使用MyBatis提供的Resources类加载mybatis的配置文件 12 Reader reader = Resources.getResourceAsReader("mybatis/mybatis-config.xml"); 13 //构建sqlSession的工厂 14 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); 15 16 SqlSession session= sessionFactory.openSession(); 17 UserMapper mapper=session.getMapper(UserMapper.class); 18 User user= mapper.GetUserByID(1); 19 System.out.println(user.toString()); 20 21 session.commit(); 22 } catch (Exception e) { 23 e.printStackTrace(); 24 } 25 26 } 27 }
标签:row exception 映射 log4j community tst develop app 模板
原文地址:https://www.cnblogs.com/jackshuo/p/11296676.html