mybatis 操作数据库
目录结构
实体类:
package domain; import java.util.Date; public class AuthOrderExprieFlag { private Integer id; private Integer expireOrderNo; private String verifyExpire; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getExpireOrderNo() { return expireOrderNo; } public void setExpireOrderNo(Integer expireOrderNo) { this.expireOrderNo = expireOrderNo; } public String getVerifyExpire() { return verifyExpire; } public void setVerifyExpire(String verifyExpire) { this.verifyExpire = verifyExpire == null ? null : verifyExpire.trim(); } @Override public String toString() { return "AuthOrderExprieFlag{" + "id=" + id + ", expireOrderNo=" + expireOrderNo + ", verifyExpire='" + verifyExpire + '\'' + '}'; } }
mapper:
package mapper; import domain.AuthOrderExprieFlag; /** * Created by tom on 2017/12/12. */ public interface AuthOrderExprieFlagMapper { public AuthOrderExprieFlag getAuthOrderExprieFlag(Integer i); }
这里mapper.AuthOrderExprieFlagMapper 对应xml 文件的namespace
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 为这个mapper指定一个唯一的namespace,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的 例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀) --> <mapper namespace="mapper.AuthOrderExprieFlagMapper"> <resultMap id="BaseResultMap" type="domain.AuthOrderExprieFlag"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="expire_order_no" jdbcType="INTEGER" property="expireOrderNo" /> <result column="verify_expire" jdbcType="VARCHAR" property="verifyExpire" /> </resultMap> <!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复 使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型 resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回 User类就是users表所对应的实体类 --> <!-- 根据id查询得到一个user对象 --> <select id="getAuthOrderExprieFlag" parameterType="int" resultMap="BaseResultMap"> select * from tt_auth_order_expire_flag where id=#{id} </select> </mapper>
数据库的配置文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <!-- 配置数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://10.16.199.72:3306/cert"/> <property name="username" value="certuser"/> <property name="password" value="certuser"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapping/AuthOrderExpireFlagMapping.xml"/> </mappers> </configuration>
最终查询的方法:
import domain.AuthOrderExprieFlag; import mapper.AuthOrderExprieFlagMapper; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.InputStream; /** * Created by tom on 2017/12/12. */ public class Test { public static void main(String[] args) { //mybatis的配置文件 String resource = "conf.xml"; //使用类加载器加载mybatis的配置文件(它也加载关联的映射文件) InputStream is = Test.class.getClassLoader().getResourceAsStream(resource); //构建sqlSession的工厂 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); //使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件) //Reader reader = Resources.getResourceAsReader(resource); //构建sqlSession的工厂 //SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); //创建能执行映射文件中sql的sqlSession SqlSession session = sessionFactory.openSession(); /** * 映射sql的标识字符串, * me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值, * getUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL */ AuthOrderExprieFlagMapper authOrderExprieFlagMapper = session.getMapper(AuthOrderExprieFlagMapper.class); //执行查询返回一个唯一user对象的sql AuthOrderExprieFlag authOrderExprieFlag = authOrderExprieFlagMapper.getAuthOrderExprieFlag(296); System.out.println(authOrderExprieFlag); } }
如果要在系统中添加日志logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- mapper 文件的包路径--> <logger name="mapper.AuthOrderExprieFlagMapper" level="debug" /> <!--log4jdbc --> <logger name="jdbc.sqltiming" level="debug"/> <logger name="com.ibatis" level="debug" /> <logger name="com.ibatis.common.jdbc.SimpleDataSource" level="debug" /> <logger name="com.ibatis.common.jdbc.ScriptRunner" level="debug" /> <logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" level="debug" /> <logger name="java.sql.Connection" level="debug" /> <logger name="java.sql.Statement" level="debug" /> <logger name="java.sql.PreparedStatement" level="debug" /> <logger name="java.sql.ResultSet" level="debug" /> <root level="info"> <appender-ref ref="console" /> <appender-ref ref="cn.iautos.manager" /> <appender-ref ref="com.ibatis" /> <appender-ref ref="rollingFile" /> </root> </configuration>
系统使用的pom 文件
<?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>com.mybatis</groupId> <artifactId>Test</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> </dependencies> </project>