| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4612 人关注过本帖
标题:spring mybatis配置
只看楼主 加入收藏
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
结帖率:40%
收藏
 问题点数:0 回复次数:13 
spring mybatis配置
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.
    xmlns:xsi="http://www. xmlns:aop="http://www.
    xmlns:tx="http://www. xmlns:context="http://www.
    xsi:schemaLocation="  
            http://www. http://www.  
            http://www. http://www.  
            http://www. http://www.  
            http://www. http://www.  
            http://www. http://www.
    default-autowire="byName" default-lazy-init="false">

    <!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。 -->

    <bean id="dataSource" class="org.
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://127.0.0.1:3309/mybatis?characterEncoding=utf8" />
        <property name="username" value="root" />
        <property name="password" value="123" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--dataSource属性指定要用到的连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!--注册Mapper方式一:configLocation属性指定mybatis的核心配置文件 -->
        <property name="configLocation" value="config/Configuration.xml" />
        <!-- 该配置文件用来指定Mapper映射文件的位置 ,如果映射文件与相应的接口同名, 且在同一路径下,那么可以不配置该选项 -->
        <!--指定实体类映射文件,可以指定同时指定某一包以及子包下面的所有配置文件, mapperLocations和configLocation有一个即可,
            当需要为实体类指定别名时,可指定configLocation属性, 再在mybatis总配置文件中采用mapper引入实体类映射文件 -->
        <!--<property name="mapperLocations" value="com/pojo/UseMapper.xml"/> -->
    </bean>

    <!-- 不须要有映射文件,需要IUserOperation接口 -->
    <!-- 注册Mapper方式一:通过sqlSessionFactory获得IUserOperation接口,配置如下: -->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例 -->
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
        <!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象,所以不需要配置文件 -->
        <property name="mapperInterface" value="dao.Interface.IUserOperation" />
    </bean>
    <bean id="CategoryMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例 -->
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
        <!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象 -->
        <property name="mapperInterface" value="dao.Interface.CategoryMapper" />
    </bean>
    <!-- 将获得的接口注入到ServiceImpl类 -->
    <bean id="UserServiceImpl1" class="server.serverImpl.userserviceimp1">
        <property name="userDao" ref="userMapper"></property>
    </bean>

    <!-- 不须要有映射文件,需要IUserOperation接口 -->
    <!-- 需要IUserOperation接口接口中要用@注解的sql语句 -->
    <!-- 注册Mapper方式二:使用自动扫描包的方式来注册各种Mapper ,获得IUserOperation接口,配置如下: -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="dao.Interface" />
    </bean>
    <bean id="UserServiceImpl2" class="server.serverImpl.userserviceimp1">
        <property name="userDao" ref="IUserOperation"></property>
    </bean>

    <!--必须要mapper映射文件 -->
    <!-- 注解mapper方式三:ServiceImpl类采用 sqlSessionTemplate或 者继承SqlSessionDaoSupport类,获取mapper映射文件中的方法 -->
    <!-- 如果mapper映射文件中采用class则自动扫描接口中注解生成映射 -->
    <!-- 如果mapper映射文件中采用resource则自动扫描映射文件生成映射 -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>
    <bean id="UserServiceImpl3" class="server.serverImpl.userserviceimp2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    <bean id="UserServiceImpl4" class="server.serverImpl.userserviceimp3">
        <property name="sqlSession" ref="sqlSessionTemplate"></property>
    </bean>


</beans>
搜索更多相关主题的帖子: version spring 
2013-11-11 17:20
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-// Config 3.0//EN"
"http://
<configuration>
    <typeAliases>
        <typeAlias alias="User" type="com.pojo.User" />
        <typeAlias alias="Article" type="com.pojo.Article" />
    </typeAliases>
    <!-- 与spring 集成之后,这些可以完全删除,数据库连接的管理交给 spring 去管理 -->
    <!-- <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://127.0.0.1:3306/mybatis?characterEncoding=utf8"
        /> <property name="username" value="root"/> <property name="password" value="password"/>
        </dataSource> </environment> </environments> -->

    <mappers>
        <mapper resource="com/pojo/UserMapper.xml" />
        <mapper resource="com/pojo/CategoryMapper.xml" />
        <!-- 通过package元素将会把指定包下面的所有Mapper接口进行注册 -->
        <!--<package name="com.tiantian.mybatis.mapperinterface"/> -->
        <!-- 通过mapper元素的resource属性可以指定一个相对于类路径的Mapper.xml文件 -->
        <!--<mapper resource="com/tiantian/mybatis/mapper/UserMapper.xml"/> -->
        <!-- 通过mapper元素的url属性可以指定一个通过URL请求道的Mapper.xml文件 -->
        <!--<mapper url="file:///E:/UserMapper.xml"/> -->
        <!-- 通过mapper元素的class属性可以指定一个Mapper接口进行注册 -->
        <!--<mapper class="com.tiantian.mybatis.mapperinterface.UserMapper"/> -->

    </mappers>
</configuration>
2013-11-11 17:28
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 

Create TABLE `category` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `catname` varchar(50) NOT NULL,
  `catdescription` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2013-11-11 17:29
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-// MyBatis Generator Configuration 1.0//EN"
  "http://

<generatorConfiguration>
  
  <!-- 配置mysql 驱动jar包路径.用了绝对路径 -->
  <classPathEntry location="E:\mywork\springmybatis\WebContent\WEB-INF\lib\mysql-connector-java-5.1.22-bin.jar" />

  <context id="yihaomen_mysql_tables" targetRuntime="MyBatis3">
  
    <!-- 为了防止生成的代码中有很多注释,比较难看,加入下面的配置控制 -->
    <commentGenerator>
      <property name="suppressAllComments" value="true" />
      <property name="suppressDate" value="true" />
    </commentGenerator>
    <!-- 注释控制完毕 -->
  
    <!-- 数据库连接 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://127.0.0.1:3309/mybatis?characterEncoding=utf8"
        userId="root"
        password="123">
    </jdbcConnection>

    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
   
    <!-- 数据表对应的model 层  -->
    <javaModelGenerator targetPackage="com.pojo" targetProject="src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
   
    <!-- sql mapper 隐射配置文件 -->
    <sqlMapGenerator targetPackage="com.pojo"  targetProject="src">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
   
    <!-- 在ibatis2 中是dao层,但在mybatis3中,其实就是mapper接口 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="dao.Interface"  targetProject="src">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
   
    <!-- 要对那些数据表进行生成操作,必须要有一个. -->
    <table schema="mybatis" tableName="category" domainObjectName="Category"
        enableCountByExample="false" enableUpdateByExample="false"
        enableDeleteByExample="false" enableSelectByExample="false"
        selectByExampleQueryId="false">     
    </table>

  </context>
</generatorConfiguration>
2013-11-11 17:29
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import
import
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

public class GenMain {
    public static void main(String[] args) {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        String genCfg = "mbgConfiguration.xml";
        File configFile = new File(GenMain.class.getResource(genCfg).getFile());
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = null;
        try {
            config = cp.parseConfiguration(configFile);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMLParserException e) {
            e.printStackTrace();
        }
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = null;
        try {
            myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
        }
        try {
            myBatisGenerator.generate(null);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
2013-11-11 17:30
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-// Mapper 3.0//EN"
"http://

<mapper namespace="dao.Interface.IUserOperation">

    <select id="selectUserByID" parameterType="int" resultType="User">
        select * from `user` where id = #{id}
    </select>
   
    <!-- 为了返回list 类型而定义的returnMap -->
    <resultMap type="User" id="resultListUser">
        <id column="id" property="id" />
        <result column="userName" property="userName" />
        <result column="userAge" property="userAge" />
        <result column="userAddress" property="userAddress" />
    </resultMap>
   
    <!-- 返回list 的select 语句,注意 resultMap 的值是指向前面定义好的 -->
    <select id="selectUsers" parameterType="string" resultMap="resultListUser">
        select * from user where userName like #{userName}
    </select>
   
   
   
    <!-- User 联合文章进行查询 方法之一的配置 (多对一的方式)  -->   
    <resultMap id="resultUserArticleList" type="Article">
        <id property="id" column="aid" />
        <result property="title" column="title" />
        <result property="content" column="content" />
        
        <association property="user" javaType="User">
            <id property="id" column="id" />
            <result property="userName" column="userName" />
            <result property="userAddress" column="userAddress" />            
        </association>        
    </resultMap>
   
    <!-- User 联合文章进行查询 方法之二的配置 (多对一的方式) -->   
    <resultMap id="resultUserArticleList-2" type="Article">
        <id property="id" column="aid" />
        <result property="title" column="title" />
        <result property="content" column="content" />        
        <association property="user" javaType="User" resultMap="resultListUser" />            
    </resultMap>
   
    <select id="getUserArticles" parameterType="int" resultMap="resultUserArticleList">
        select user.id,user.userName,user.userAddress,article.id aid,article.title,article.content from user,article
        where user.id=article.userid and user.id=#{id}
    </select>
   
   
    <!--执行增加操作的SQL语句。id和parameterType  
    分别与IUserOperation接口中的addUser方法的名字和  
    参数类型一致。以#{name}的形式引用Student参数  
    的name属性,MyBatis将使用反射读取Student参数  
    的此属性。#{name}中name大小写敏感。引用其他  
    的gender等属性与此一致。seGeneratedKeys设置  
    为"true"表明要MyBatis获取由数据库自动生成的主  
    键;keyProperty="id"指定把获取到的主键值注入  
    到Student的id属性-->
    <insert id="addUser" parameterType="User"
            useGeneratedKeys="true" keyProperty="id">
        insert into user(userName,userAge,userAddress)  
        values(#{userName},#{userAge},#{userAddress})  
    </insert>
   
    <update id="updateUser" parameterType="User" >
        update user set userName=#{userName},userAge=#{userAge},userAddress=#{userAddress} where id=#{id}
    </update>
   
    <delete id="deleteUser" parameterType="int">
        delete from user where id=#{id}
    </delete>
   
</mapper>
2013-11-11 17:31
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
package dao.Interface;

import com.pojo.Article;
import com.pojo.User;
import java.util.List;

import org.apache.ibatis.annotations.Select;




public interface IUserOperation {
    //@Select("select * from `user` where id = #{id}")
    public User selectUserByID(int id);

   
}
2013-11-11 17:31
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
package server.serverImpl;

import com.pojo.User;

import dao.Interface.IUserOperation;
import server.Interface.IUserService;

public class userserviceimp1 implements IUserService {
    private IUserOperation userDao;

    public IUserOperation getUserDao() {
        return userDao;
    }

    public void setUserDao(IUserOperation userDao) {
        this.userDao = userDao;
    }



    public User selectUserByID(int id) {
        return this.userDao.selectUserByID(id);
    }
}
2013-11-11 17:31
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
package server.serverImpl;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.pojo.User;

import server.Interface.IUserService;

public class userserviceimp2 extends SqlSessionDaoSupport implements IUserService {
    public User selectUserByID(int id) {
        return getSqlSession().selectOne("dao.Interface.IUserOperation.selectUserByID",id);
    }

}
2013-11-11 17:32
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
package server.serverImpl;

import org.mybatis.spring.SqlSessionTemplate;

import com.pojo.User;

import server.Interface.IUserService;

public class userserviceimp3 implements IUserService {
    public SqlSessionTemplate sqlSession;
    public void setSqlSession(SqlSessionTemplate sqlSession) {
         this.sqlSession = sqlSession;
    }
    public User selectUserByID(int id) {
        return sqlSession.selectOne("dao.Interface.IUserOperation.selectUserByID",id);
    }

}
2013-11-11 17:32
快速回复:spring mybatis配置
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023755 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved