zoukankan      html  css  js  c++  java
  • java框架--spring+mybatis 注解整合

    package cn.sxt.dao;
    
    import java.util.List;
    
    import org.apache.ibatis.annotations.Select;
    
    import cn.sxt.vo.User;
    
    public interface UserMapper {
        
        @Select("select * from user")
        public List<User> selectUser(); 
    }
    package cn.sxt.service;
    
    import java.util.List;
    
    import cn.sxt.vo.User;
    
    public interface UserService {
        public List<User> selectUser(); 
    }
    package cn.sxt.service.impl;
    
    import java.util.List;
    
    import cn.sxt.dao.UserMapper;
    import cn.sxt.service.UserService;
    import cn.sxt.vo.User;
    
    public class UserServiceImpl implements UserService{
        private UserMapper userMapper = null;
        public void setUserMapper(UserMapper userMapper) {
            this.userMapper = userMapper;
        }
        @Override
        public List<User> selectUser() {
            System.out.println(userMapper);
            return userMapper.selectUser();
        }
    }
    package cn.sxt.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import cn.sxt.service.UserService;
    
    public class Test {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            UserService userService = (UserService)context.getBean("userService");
            System.out.println(userService.selectUser().size());
        }
    }
    package cn.sxt.vo;
    
    public class User {
        private int id;
        private String name;
        private String pwd;
        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 String getPwd() {
            return pwd;
        }
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
        <!-- 配置数据源 -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/orcl"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
        </bean>
        <!-- 声明式事务配置 开始 -->
        <!-- 配置事务管理器 -->
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <!-- 配置事务通知 -->
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <!-- 配置哪些方法使用什么样的事务,配置事务的传播特性 -->
                <tx:method name="add" propagation="REQUIRED"/>
                <tx:method name="insert" propagation="REQUIRED"/>
                <tx:method name="update" propagation="REQUIRED"/>
                <tx:method name="delete" propagation="REQUIRED"/>
                <tx:method name="remove*" propagation="REQUIRED"/>
                <tx:method name="get" read-only="true"/>
                <tx:method name="*" propagation="REQUIRED"/>
            </tx:attributes>
        </tx:advice>
        <aop:config>
            <aop:pointcut expression="execution(* cn.sxt.service.impl.*.*(..))" id="pointcut"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
        </aop:config>
        <!-- 声明式事务配置 结束 -->
        <!-- 配置sqlSessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
            <property name="mapperInterface" value="cn.sxt.dao.UserMapper"/>
            <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        </bean>
        <bean id="userService" class="cn.sxt.service.impl.UserServiceImpl">
            <property name="userMapper" ref="userMapper"/>
        </bean>
    </beans>
    <?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>
        <typeAliases>
            <package name="cn.sxt.vo"/>
        </typeAliases>
        
        <mappers>
            <package name="cn.sxt.dao.UserMapper"/>
        </mappers> 
    </configuration>
    /*
    Navicat MySQL Data Transfer
    
    Source Server         : test
    Source Server Version : 50520
    Source Host           : localhost:3306
    Source Database       : orcl
    
    Target Server Type    : MYSQL
    Target Server Version : 50520
    File Encoding         : 65001
    
    Date: 2018-01-07 12:32:39
    */
    
    SET FOREIGN_KEY_CHECKS=0;
    
    -- ----------------------------
    -- Table structure for `user`
    -- ----------------------------
    DROP TABLE IF EXISTS `user`;
    CREATE TABLE `user` (
      `name` varchar(10) DEFAULT NULL,
      `id` int(20) NOT NULL AUTO_INCREMENT,
      `pwd` varchar(30) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
    
    -- ----------------------------
    -- Records of user
    -- ----------------------------
    INSERT INTO `user` VALUES ('澶т緺', '1', '4444');
    INSERT INTO `user` VALUES ('澶т緺', '2', '4444');

       打印:  9 

  • 相关阅读:
    js showModalDialog参数传递
    1:N 关系
    1:N 关系 视图查找
    设置IE主页和添加收藏夹的功能
    GridView和DataFormatString网站技术
    JS 的table属性操作,
    GridView帮定数据显示数据的技巧
    后台取相同name值的问题
    赶集网CEO杨浩涌:倒闭没那么容易
    用户数据泄露案告破:嫌疑人已抓 CSDN受到警告
  • 原文地址:https://www.cnblogs.com/ou-pc/p/8228091.html
Copyright © 2011-2022 走看看