搭建mybatis开发环境的步骤和代码

|--主要步骤

1、下载mybatis的jar包并导入工程

2、编写mybatis的核心配置文件---工程名-mybatis-config.xml  

3、创建实体类POJO

4、Dao文件夹下创建  实体类的mapper.java接口和配置实体类的mapper.xml文件

5、创建测试类,看有没有问题

 

|--工程结构

搭建mybatis开发环境的步骤和代码_第1张图片

 

 

|--关键代码

1 jdbc.driver=com.mysql.cj.jdbc.Driver
2 jdbc.url=jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL
3 jdbc.username=root
4 jdbc.password=long
resources文件夹下的database.properties文件--里面放的是连接数据库的配置信息
 
 1 ### 设置Logger输出级别和输出目的地 ###
 2 
 3 log4j.rootLogger=debug,stdout,logfile
 4 
 5 ### 把日志信息输出到控制台 ### 注销第一行,使用的时候就没那么多烦人的日志了
 6 #log4j.appender.stdout=org.apache.log4j.ConsoleAppender
 7 log4j.appender.stdout.Target=System.err
 8 log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
 9 
10 
11 ### 把日志信息输出到文件:jbit.log ###
12 log4j.appender.logfile=org.apache.log4j.FileAppender
13 log4j.appender.logfile.File=jbit.log
14 log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
15 log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n
16 
17 
18 ###显示SQL语句部分
19 log4j.logger.com.ibatis=DEBUG
20 log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
21 log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
22 log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
23 log4j.logger.java.sql.Connection=DEBUG
24 log4j.logger.java.sql.Statement=DEBUG
25 log4j.logger.java.sql.PreparedStatement=DEBUG
resources文件夹下的log4j.properties
 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 
 6 
 7 
 8 <configuration>
 9 
10     
11     <properties resource="database.properties"/>
12 
13     
14     <environments default="dev">
15         
16         <environment id="dev">
17             
18             <transactionManager type="JDBC">transactionManager>
19             
20             <dataSource type="POOLED">
21                 
22                 <property name="driver" value="${jdbc.driver}"/>
23                 <property name="url" value="${jdbc.url}"/>
24                 <property name="username" value="${jdbc.username}"/>
25                 <property name="password" value="${jdbc.password}"/>
26             dataSource>
27         environment>
28         
29     environments>
30 
31 
32 <mappers>
33 
34     <package name="cn.kgc.dao"/>
35 mappers>
36 
37 configuration>
resources文件夹下的mybatis全局配置文件mybatis-work.xml
  1 package cn.kgc.pojo;
  2 
  3 public class User {
  4     private Integer id; //id
  5     private String userCode; //用户编码
  6     private String userName; //用户名称
  7     private String userPassword; //用户密码
  8     private Integer gender;  //性别
  9     private String birthday;  //出生日期
 10     private String phone;   //电话
 11     private String address; //地址
 12     private Integer userRole;    //用户角色
 13     private Integer createdBy;   //创建者
 14     private String creationDate; //创建时间
 15     private Integer modifyBy;     //更新者
 16     private String modifyDate;   //更新时间
 17 
 18     public Integer getId() {
 19         return id;
 20     }
 21 
 22     public void setId(Integer id) {
 23         this.id = id;
 24     }
 25 
 26     public String getUserCode() {
 27         return userCode;
 28     }
 29 
 30     public void setUserCode(String userCode) {
 31         this.userCode = userCode;
 32     }
 33 
 34     public String getUserName() {
 35         return userName;
 36     }
 37 
 38     public void setUserName(String userName) {
 39         this.userName = userName;
 40     }
 41 
 42     public String getUserPassword() {
 43         return userPassword;
 44     }
 45 
 46     public void setUserPassword(String userPassword) {
 47         this.userPassword = userPassword;
 48     }
 49 
 50     public Integer getGender() {
 51         return gender;
 52     }
 53 
 54     public void setGender(Integer gender) {
 55         this.gender = gender;
 56     }
 57 
 58     public String getBirthday() {
 59         return birthday;
 60     }
 61 
 62     public void setBirthday(String birthday) {
 63         this.birthday = birthday;
 64     }
 65 
 66     public String getPhone() {
 67         return phone;
 68     }
 69 
 70     public void setPhone(String phone) {
 71         this.phone = phone;
 72     }
 73 
 74     public String getAddress() {
 75         return address;
 76     }
 77 
 78     public void setAddress(String address) {
 79         this.address = address;
 80     }
 81 
 82     public Integer getUserRole() {
 83         return userRole;
 84     }
 85 
 86     public void setUserRole(Integer userRole) {
 87         this.userRole = userRole;
 88     }
 89 
 90     public Integer getCreatedBy() {
 91         return createdBy;
 92     }
 93 
 94     public void setCreatedBy(Integer createdBy) {
 95         this.createdBy = createdBy;
 96     }
 97 
 98     public String getCreationDate() {
 99         return creationDate;
100     }
101 
102     public void setCreationDate(String creationDate) {
103         this.creationDate = creationDate;
104     }
105 
106     public Integer getModifyBy() {
107         return modifyBy;
108     }
109 
110     public void setModifyBy(Integer modifyBy) {
111         this.modifyBy = modifyBy;
112     }
113 
114     public String getModifyDate() {
115         return modifyDate;
116     }
117 
118     public void setModifyDate(String modifyDate) {
119         this.modifyDate = modifyDate;
120     }
121 }
POJO-User
1 package cn.kgc.dao;
2 
3 public interface UserMapper {
4     /**
5      *
6      * @return 用户总数
7      */
8     int count();
9 }
UserMapper.java
 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 
 8 <mapper namespace="cn.kgc.dao.UserMapper">
 9 
10 
11 
12 <select id="count" resultType="int">
13 -- select标签里面写sql语句
14      select count(id)  from smbms_user
15 select>
16 mapper>
UserMapper.xml

 

你可能感兴趣的:(搭建mybatis开发环境的步骤和代码)