pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.bdqn</groupId> <artifactId>UserDemo</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> 导入Junit包 <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> 导入打印日志包 <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> 导入mybatis包 <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> 导入mysql包 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.28</version> </dependency> </dependencies> 指明文件java代码的文件路径 <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
//接口 package cn.bdqn.dao; import cn.bdqn.entity.User; import java.util.List; public interface UserMapper{ Integer countAllID();//记录有多少个id的个数 List<User> queryAllUser();//查询全部信息 }
//命名空间 resultType结果类型 resultMap结果匹配 <mapper namespace="cn.bdqn.dao.UserMapper"> <select id="countAllID" resultType="int"> SELECT COUNT(id) FROM t_user </select> <select id="queryAllUser" resultMap="userMapper"> SELECT * FROM t_user ORDER BY id DESC </select> <resultMap id="userMapper" type="cn.bdqn.entity.User"> <id column="id" property="id"></id> <result property="userName" column="user_name" ></result> <result property="nickName" column="nick_name" ></result> <result property="password" column="password"></result> </resultMap> </mapper>
//实体类
package cn.bdqn.entity; public class User { private int id; private String userName,password,nickName; @Override public String toString() { return "User{" + "id=" + id + ", userName='" + userName + '\'' + ", password='" + password + '\'' + ", nickName='" + nickName + '\'' + '}'; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
工具类
public class mybatisUtil { private static SqlSessionFactory factory; static { try { InputStream io = Resources.getResourceAsStream("mybatis-config.xml"); factory= new SqlSessionFactoryBuilder().build(io); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession createSqlsession(){ return factory.openSession(); } public static void closeAll(SqlSession sqlSession){ if (sqlSession != null) { sqlSession.close(); } } }
<configuration> <properties resource="dataBase.properties" ></properties> <environments default="user"> <environment id="user"> <transactionManager type="jdbc"></transactionManager> <dataSource type="pooled"> <property name="driver" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${username}"></property> <property name="password" value="${password}"></property> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/bdqn/dao/UserMapper.xml"></mapper> </mappers> </configuration>