zoukankan      html  css  js  c++  java
  • 我的第九个java程序--spring和mybatis整合(java project)

    思路:入口程序读spring的配置文件-配置文件注入给程序bean--程序拿到bean以操作对象的手法查出程序

    入口程序HelloWorld.java

    package HelloWorld;
    
    
    
    
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    
    import HelloWorld.UserDao;
    import HelloWorld.User;
    
    public class HelloWorld {
        
        /**
         * @param args
         */
        
    
        
        public static void main(String[] args) {
            
            ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:ApplicationContext.xml");
            UserDao userDao=(UserDao) ctx.getBean("userDao");
            
            User user=new User();
            user.setId(1);
            System.out.println(userDao.getUser(user).toString());
           
    
        }
    
    }
    ----------------------------------------------------------------------------------------------------------------
    import HelloWorld.UserDao;//把bean变成对象操作 import HelloWorld.User;//把字段变成对象操作

    ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:ApplicationContext.xml");//读取spring的配置文件------1

    UserDao userDao=(UserDao) ctx.getBean("userDao");//拿到配置文件的bean,并把它变成对象------2
    User user=new User();
    user.setId(1);//设置对象的属性,即设置字段id为1
    System.out.println(userDao.getUser(user).toString());//往userDao对象的getUser方法传值,值为user
    ----------------------------------------------------------------------------------------------------------------

    1和2合并指向的就是ApplicationContext.xml这个文件,过去看看
    <?xml version="1.0" encoding="UTF-8"?>
    <beans  
        xmlns="http://www.springframework.org/schema/beans"  
        xmlns:tx="http://www.springframework.org/schema/tx"  
        xmlns:p="http://www.springframework.org/schema/p"  
        xmlns:aop="http://www.springframework.org/schema/aop"   
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
        http://www.springframework.org/schema/aop    
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
    <!-- 配置数据源-->  
        <bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName">  
            <value>com.mysql.jdbc.Driver</value>  
        </property>  
        <property name="url">  
            <value>jdbc:mysql://localhost:3306/mybatis</value>  
           <!--springmybaitis是我的数据库  -->
        </property>  
        <property name="username">  
            <value>root</value>  
        </property>  
        <property name="password">  
            <value>root</value>  
        </property>  
        </bean>  
        
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
         <property name="dataSource" ref="jdbcDataSource" />  
         <property name="configLocation" value="classpath:conf.xml"></property>  
     </bean>  
     <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">  
        <property name="mapperInterface" value="HelloWorld.UserDao"></property>  
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>  
     </bean>  
     
    </beans>

    ----------------------------------------------------------------------------------------------------------------

    配合了数据源,加载了一个conf.xml,userDao这个bean在最下面

    org.mybatis.spring.mapper.MapperFactoryBean好像依赖于一个jar,不然老是报

    ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:ApplicationContext.xml");

    其实不是程序的错,而是配置文件的错,主要是它报的错没办法指向配置文件,但是根据具体的报错信息(一行一行的看的。。。)加白度,大概了解到是配置文件里有错了,且是少了一个jar包这样的错

    org.springframework.transaction-3.1.0.RC1.jar这个包

    我把3.2的包下载完了,以为不会缺jar包了,可能是抄的别人代码的缘故,就是缺org.springframework.transaction-3.1.0.RC1.jar这个包,3.1的?。。。

    ------------------------------------------------------------------------------------------------------------------------

    <property name="configLocation" value="classpath:conf.xml"></property>  

    红字是主线,顺着主线往下走,找conf.xml这个配置文件

    <?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>
          
         
         <mappers>
             <!-- 注册userMapper.xml文件, 
             userMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/userMapper.xml-->
             <mapper resource="HelloWorld/UserDao.xml"/>
         </mappers>
         
     </configuration>
    <mapper resource="HelloWorld/UserDao.xml"/>

    <?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="HelloWorld.UserDao">
        <!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复
        使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型
        resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回
        User类就是users表所对应的实体类
        -->
        <!-- 
            根据id查询得到一个user对象
         -->
        <select id="getUser" parameterType="int" 
            resultType="HelloWorld.User">
            select * from users where id=#{id}
        </select>
    </mapper>

    终于要查询了。。。

    补下UserDao、User

    package HelloWorld;
    
    import HelloWorld.User;
    
    public interface UserDao {
        public User getUser(User user);
        public void addUser(User user);
        public void updateUser(User user);
        public void deleteUser(int UserId);
    }
    package HelloWorld;
    
    /**
     * @author gacl
     * users表所对应的实体类
     */
    public class User {
    
        //实体类的属性和表的字段名称一一对应
        private int id;
        private String name;
        private int age;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
        }
    }

    结构图:





    查看报错就是熟悉它的过程,能学到很多东西

    效果:



    至少知道程序走的哪了,在哪出的错,为什么会出错。。。
  • 相关阅读:
    27 Spring Cloud Feign整合Hystrix实现容错处理
    26 Spring Cloud使用Hystrix实现容错处理
    25 Spring Cloud Hystrix缓存与合并请求
    24 Spring Cloud Hystrix资源隔离策略(线程、信号量)
    23 Spring Cloud Hystrix(熔断器)介绍及使用
    22 Spring Cloud Feign的自定义配置及使用
    21 Spring Cloud使用Feign调用服务接口
    20 Spring Cloud Ribbon配置详解
    19 Spring Cloud Ribbon自定义负载均衡策略
    18 Spring Cloud Ribbon负载均衡策略介绍
  • 原文地址:https://www.cnblogs.com/hellowzd/p/5022331.html
Copyright © 2011-2022 走看看