mybatis环境搭建和开发步骤
环境搭建
第一步:导入jar包
第二步:导入核心配置文件(mybatis-config.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 6 <configuration> 7 <settings> 8 <!-- 表示使用log4j来处理日志 --> 9 <setting name="logImpl" value="LOG4J"/> 10 </settings> 11 12 <!-- 环境变量 连接数据库的信息 --> 13 <environments default="dev"> 14 <environment id="dev"> 15 <transactionManager type="JDBC"></transactionManager> 16 <dataSource type="POOLED"> 17 <property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms?zeroDateTimeBehavior=convertToNull"/> 18 <property name="driver" value="com.mysql.jdbc.Driver"/> 19 <property name="username" value="root"/> 20 <property name="password" value="199808025"/> 21 </dataSource> 22 </environment> 23 </environments> 24 25 <!-- 注册Mapper(映射)文件 --> 26 <mappers> 27 <mapper resource="com/smbms/dao/UserMapper.xml"/> 28 <mapper resource="com/smbms/dao/ProviderMapper.xml"/> 29 <mapper resource="com/smbms/dao/AddressMapper.xml"/> 30 <mapper resource="com/smbms/dao/BillMapper.xml"/> 31 <mapper resource="com/smbms/dao/RoleMapper.xml"/> 32 </mappers> 33 34 </configuration>
开发步骤
第一步:表
第二步:pojo(实体类)
1 package com.smbms.entity; 2 3 import java.util.Date; 4 5 public class User { 6 private int id; 7 private String userCode; 8 private String userName; 9 private String userPassword; 10 private int gender; 11 private Date birthday; 12 private String phone; 13 private String address; 14 private int userRole; 15 private int createdBy; 16 private Date creationDate; 17 private int modifyBy; 18 private Date modifyDate; 19 public User() { 20 21 } 22 23 public User(int id, String userCode, String userName, String userPassword, int gender, Date birthday, String phone, 24 String address, int userRole, int createdBy, Date creationDate, int modifyBy, Date modifyDate) { 25 super(); 26 this.id = id; 27 this.userCode = userCode; 28 this.userName = userName; 29 this.userPassword = userPassword; 30 this.gender = gender; 31 this.birthday = birthday; 32 this.phone = phone; 33 this.address = address; 34 this.userRole = userRole; 35 this.createdBy = createdBy; 36 this.creationDate = creationDate; 37 this.modifyBy = modifyBy; 38 this.modifyDate = modifyDate; 39 } 40 41 42 public int getId() { 43 return id; 44 } 45 public void setId(int id) { 46 this.id = id; 47 } 48 public String getUserCode() { 49 return userCode; 50 } 51 public void setUserCode(String userCode) { 52 this.userCode = userCode; 53 } 54 public String getUserName() { 55 return userName; 56 } 57 public void setUserName(String userName) { 58 this.userName = userName; 59 } 60 public String getUserPassword() { 61 return userPassword; 62 } 63 public void setUserPassword(String userPassword) { 64 this.userPassword = userPassword; 65 } 66 public int getGender() { 67 return gender; 68 } 69 public void setGender(int gender) { 70 this.gender = gender; 71 } 72 public Date getBirthday() { 73 return birthday; 74 } 75 public void setBirthday(Date birthday) { 76 this.birthday = birthday; 77 } 78 public String getPhone() { 79 return phone; 80 } 81 public void setPhone(String phone) { 82 this.phone = phone; 83 } 84 public String getAddress() { 85 return address; 86 } 87 public void setAddress(String address) { 88 this.address = address; 89 } 90 public int getUserRole() { 91 return userRole; 92 } 93 public void setUserRole(int userRole) { 94 this.userRole = userRole; 95 } 96 public int getCreatedBy() { 97 return createdBy; 98 } 99 public void setCreatedBy(int createdBy) { 100 this.createdBy = createdBy; 101 } 102 public int getModifyBy() { 103 return modifyBy; 104 } 105 public void setModifyBy(int modifyBy) { 106 this.modifyBy = modifyBy; 107 } 108 109 public Date getCreationDate() { 110 return creationDate; 111 } 112 113 public void setCreationDate(Date creationDate) { 114 this.creationDate = creationDate; 115 } 116 117 public Date getModifyDate() { 118 return modifyDate; 119 } 120 121 public void setModifyDate(Date modifyDate) { 122 this.modifyDate = modifyDate; 123 } 124 }
第三步:接口
1 package com.smbms.dao; 2 3 import java.util.List; 4 5 import com.smbms.entity.User; 6 7 public interface UserMapper { 8 //查询总共有多少记录 9 public int count(); 10 //查询所有 11 public List<User> findAll(); 12 }
第四步:映射文件xxxMapper.xml(dao层)
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 <mapper namespace="com.smbms.dao.UserMapper"> 7 <!-- 记录总条数 --> 8 <select id="count" resultType="int"> 9 select count(1) from smbms_user; 10 </select> 11 <!-- 查询所有 --> 12 <select id="findAll" resultType="com.smbms.entity.User"> 13 select * from smbms_user; 14 </select> 15 </mapper>
第五步:注册映射文件
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 <!-- 注册Mapper(映射)文件 --> 7 <mappers> 8 <mapper resource="com/smbms/dao/UserMapper.xml"/> 9 <mapper resource="com/smbms/dao/ProviderMapper.xml"/> 10 <mapper resource="com/smbms/dao/AddressMapper.xml"/> 11 <mapper resource="com/smbms/dao/BillMapper.xml"/> 12 <mapper resource="com/smbms/dao/RoleMapper.xml"/> 13 </mappers> 14 15 </configuration>
第六步:测试
1 package com.smbms.dao; 2 3 import java.util.List; 4 5 import org.apache.ibatis.session.SqlSession; 6 import org.apache.log4j.Logger; 7 import org.junit.Test; 8 9 import com.smbms.entity.User; 10 import com.smbms.util.MybatisUtil; 11 12 public class TestUserMapper { 13 14 private Logger log=Logger.getLogger(TestUserMapper.class); 15 16 @Test 17 public void count(){ 18 //SQL--->connection--->SqlSession 19 //--->SqlSSessionfactory 20 21 // InputStream is; 22 // SqlSession sqlSession=null; 23 // try { 24 // is=Resources.getResourceAsStream("mybatis-config.xml"); 25 // //获取sqlsession工厂 26 // SqlSessionFactory ssf=new SqlSessionFactoryBuilder().build(is); 27 // //获取SQLSession==connection 28 // sqlSession=ssf.openSession(); 29 // 30 // //执行的sql 31 // int count=sqlSession.selectOne("com.smbms.dao.UserMapper.count"); 32 // log.debug(count); 33 // } catch (IOException e) { 34 // e.printStackTrace(); 35 // }finally { 36 // if(sqlSession!=null){ 37 // sqlSession.close(); 38 // } 39 // } 40 //执行SQLSession 41 SqlSession sqlSession=MybatisUtil.getSqlSession(); 42 //执行的sql 43 // int count=sqlSession.selectOne("com.smbms.dao.UserMapper.count"); 44 UserMapper userMapper=sqlSession.getMapper(UserMapper.class); 45 int count=userMapper.count(); 46 log.debug(count); 47 //关闭连接 48 MybatisUtil.close(sqlSession); 49 } 50 51 @Test 52 public void findAll(){ 53 SqlSession sqlSession=MybatisUtil.getSqlSession(); 54 UserMapper um=sqlSession.getMapper(UserMapper.class); 55 List<User> users=um.findAll(); 56 for(User user:users){ 57 log.debug(user.getUserName()); 58 } 59 } 60 }
运行效果图: