spring3+mybatis3+mysql

玩了一下mybatis,网上源码说的不太清楚。自己写了一下。希望对大家有用(spring3+mybatis3+mysql)

实体类:

spring3+mybatis3+mysql
spring3+mybatis3+mysql
package org.my.entity;

public class User {
    private Integer id;
    private String name;
    private Integer age;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}
spring3+mybatis3+mysql
spring3+mybatis3+mysql

mapper:

spring3+mybatis3+mysql
spring3+mybatis3+mysql
package org.my.maper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.my.entity.User;

public interface UserMapper {
    public void insertUser(User user);
    @Select("SELECT * FROM user WHERE id = #{id}") 
    public User getUser(@Param("id")Integer id);
}
spring3+mybatis3+mysql
spring3+mybatis3+mysql

dao:

spring3+mybatis3+mysql
spring3+mybatis3+mysql
package org.my.dao;

import org.my.entity.User;

public interface UserDao {
    public User getUserById(User user);
}
spring3+mybatis3+mysql
spring3+mybatis3+mysql

impl(dao实现):

spring3+mybatis3+mysql
spring3+mybatis3+mysql
package org.my.impl;

import org.my.dao.UserDao;
import org.my.entity.User;
import org.my.maper.UserMapper;

public class UserDaoImpl implements UserDao {
    private UserMapper userMapper;
    public void setUserMapper(UserMapper userMapper) { 
        this.userMapper = userMapper; 
    } 
    @Override
    public User getUserById(User user) {
        return userMapper.getUser(user.getId());
    }

}

spring3+mybatis3+mysql
spring3+mybatis3+mysql

test测试类:

spring3+mybatis3+mysql
spring3+mybatis3+mysql
package org.my;

import org.my.dao.UserDao;
import org.my.entity.User;
import org.my.impl.UserDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(  
        "beans.xml"); 
        UserDao userDao = (UserDaoImpl)ctx.getBean("userDaoImpl");
        User user = new User();
        user.setId(1);
        User u = userDao.getUserById(user);
        System.out.println(u.getName());
    }
    
}
spring3+mybatis3+mysql
spring3+mybatis3+mysql

 

beans.xml:

spring3+mybatis3+mysql
spring3+mybatis3+mysql
<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
                    ">

    <context:property-placeholder location="jdbc.properties" />
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
    </bean>
    <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="org.my.maper.UserMapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    <bean id="userDaoImpl" class="org.my.impl.UserDaoImpl">
        <property name="userMapper" ref="userMapper" />
    </bean>


</beans>
spring3+mybatis3+mysql
spring3+mybatis3+mysql

jdbc.proerties:

spring3+mybatis3+mysql
driver=com.mysql.jdbc.Driver
username=root
password=root
url=jdbc\:mysql\://localhost\:3306/test
  
spring3+mybatis3+mysql

 

接下来是项目截图:

spring3+mybatis3+mysql

jar包:

 

spring3+mybatis3+mysql

希望大家按照这个配置,能成功配置好

 

 

spring3+mybatis3+mysql,布布扣,bubuko.com

spring3+mybatis3+mysql

上一篇:Oracle日期的加减运算


下一篇:PHP连接SQLServer