【Java EE】搭建MyBatis的环境

来源:互联网 发布:手机淘宝首页psd模板 编辑:程序博客网 时间:2024/05/23 13:56
  1. 导入jar包
  2. 创建MyBatis的主配置文件:配置了数据库连接的数据源
    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configurationPUBLIC "-//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="数据库驱动路径"/> <property name="url" value="数据库URL"/> <property name="username" value="用户名"/> <property name="password" value="密码"/> </dataSource> </environment> </environments><!-- 对映射文件进行注册 --><mappers>     <mapper resource="映射文件路径"/></mappers> </configuration>
  3. 创建系统日志的配置文件:
    ##    Copyright 2009-2012 the original author or authors.##    Licensed under the Apache License, Version 2.0 (the "License");#    you may not use this file except in compliance with the License.#    You may obtain a copy of the License at##       http://www.apache.org/licenses/LICENSE-2.0##    Unless required by applicable law or agreed to in writing, software#    distributed under the License is distributed on an "AS IS" BASIS,#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.#    See the License for the specific language governing permissions and#    limitations under the License.#### Global logging configurationlog4j.rootLogger=DEBUG, stdout### Uncomment for MyBatis logginglog4j.logger.org.apache.ibatis=DEBUG### Console output...log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

    Log4j的打印级别(由低到高)
       DEBUG:调试级   任何信息都会被打印
       INFO: 信息级   打印除了DEBUG级别以外的所有信息
       WARN: 警告级   打印警告及其以上级别的信息
       ERROR: 错误级   打印错误及其以上级别的信息
       FATAL:致命级   最高级
  4. 创建配置映射文件:
    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- namespace命名空间,作用就是对sql进行分类化管理,不能重名 --><mapper namespace="test">    <!-- 查询 --><!-- 查询结果的类型:无论是一条记录还是多条记录,返回的是po对象而不是list --><select id="findUserById" parameterType="int" resultType="com.neuedu.pojo.User">SELECT * FROM T_USER WHERE ID = #{value}</select><select id="findUserByName" parameterType="String" resultType="com.neuedu.pojo.User">select * from t_user where userName like '%${value}%'</select><!-- 添加 --><insert id="insertUser" parameterType="com.neuedu.pojo.User">insert into t_user(id,username,birthday,address) values (seq_user.nextval,#{userName},#{birthDay},#{address})</insert><!-- 删除 --><delete id="deleteUser" parameterType="int">delete from t_user where id = #{id}</delete><!-- 修改 --><update id="updateUser" parameterType="com.neuedu.pojo.User" >update t_user set username=#{userName},birthday=#{birthDay},address=#{address} where id = #{id}</update></mapper>


0 0