Mybatis框架
Posted mcl2238973568
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis框架相关的知识,希望对你有一定的参考价值。
- 加入Mybatis相应的架包并解压
- 创建bean包,在bean包下创建数据库相对应的实体类(创建get、set)
- 创建dao包,在dao包下创建接口实体类的操作方法
- 创建resources文件夹,在该文件夹下创建Mybatis的配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 <!-- 数据源的信息:数据库的信息。 --> 5 <configuration> 6 <environments default="development"> 7 <environment id="development"> 8 <transactionManager type="JDBC" /> 9 <dataSource type="POOLED"> 10 <property name="driver" value="com.mysql.jdbc.Driver" /> 11 <property name="url" value="jdbc:mysql://localhost:3306/mybatis" /> 12 <property name="username" value="root" /> 13 <property name="password" value="root" /> 14 </dataSource> 15 </environment> 16 </environments> 17 18 </configuration>
- 可以将数据源信息抽取到属性文件jdbc.properties中
# 数据源信息 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis username=root password=root
- 创建映射包mapper,在该包下创建实体类的映射文件
<?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="com.zhiyou100.mcl.mapper.UsersMapper"> <select id="getUser" parameterType="int" resultType="com.zhiyou100.mcl.bean.Users"> select * from users where id=#id </select> <insert id="addUser" parameterType="com.zhiyou100.mcl.bean.Users"> insert into users(name,age) values(#name,#age) </insert> <delete id="deleteUser" parameterType="int"> delete from users where id=#id </delete> <update id="updateUser" parameterType="com.zhiyou100.mcl.bean.Users"> update users set name=#name,age=#age where id=#id </update> <select id="selectAll" resultType="Users"> select * from users </select> <select id="selectByAge" parameterType="map" resultType="Users"> <![CDATA[select * from users where age>=#min and age<=#max]]> </select> </mapper>
- 把属性文件引入到mybatis的配置文件conf.xml中 使用$
- 将映射文件放入到配置文件中<mappers></mappers>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 <configuration> 5 <!-- 引入属性文件 --> 6 <properties resource="jdbc.properties"></properties> 7 <!-- 为实体类起别名 --> 8 <typeAliases> 9 <!-- <typeAlias type="com.zhiyou100.mcl.bean.Users" alias="user"/> --> 10 <package name="com.zhiyou100.mcl.bean"/> 11 </typeAliases> 12 <environments default="development"> 13 <environment id="development"> 14 <transactionManager type="JDBC" /> 15 <dataSource type="POOLED"> 16 <property name="driver" value="$driver" /> 17 <property name="url" 18 value="$url" /> 19 <property name="username" value="$username" /> 20 <property name="password" value="$password" /> 21 </dataSource> 22 </environment> 23 </environments> 24 <-- 多个映射文件放入到同一mappers个里--> 25 <mappers> 26 <mapper resource="com/zhiyou100/mcl/mapper/UsersMapper.xml"/> 27 <mapper resource="com/zhiyou100/mcl/mapper/OrderMapper.xml"/> 28 <mapper resource="com/zhiyou100/mcl/mapper/ClazzMapper.xml"/> 29 </mappers> 30 </configuration>
- 可以为实体类起别名,但是不推荐使用,容易混淆
- 加入日志信息,方便快速查询错误所在----导入架包log4j.properties。在resources文件夹创建log4j.properties文件
log4j.properties, log4j.rootLogger=DEBUG, Console #Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n log4j.logger.java.sql.ResultSet=INFO log4j.logger.org.apache=INFO log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
- 测试
以上是关于Mybatis框架的主要内容,如果未能解决你的问题,请参考以下文章