Spring+Mybatis+mysql配置

  1. mybatis的映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.lky.dao.UserMapper">
    <insert id="add" parameterType="com.lky.model.User">
        insert into s_user(user_name,user_birthday,user_salary) values(#{name},#{birthday},#{salary})
    </insert>

    <update id="update" parameterType="com.lky.model.User">
        update s_user set user_name=#{name},user_birthday=#{birthday},user_salary=#{salary} where user_id=#{id}
    </update>

    <delete id="delete" parameterType="int">
        delete from s_user where user_id=#{id}
    </delete>

    <select id="findById" parameterType="int" resultType="com.lky.model.User">
        select user_id id,user_name name,user_birthday birthday,user_salary salary from s_user where user_id=#{id}
    </select>

    <select id="findAll" resultType="com.lky.model.User">
        select user_id id,user_name name,user_birthday birthday,user_salary salary from s_user
    </select>

</mapper>

2.mybatis的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration SYSTEM "http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
    <mappers>
        <mapper resource="com/lky/dao/UserMapper.xml"/>
    </mappers>
</configuration>

3.Spring的配置文件

<?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">
    <!-- 配置数据源   DriverManagerDataSource-->
    <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://10.21.25.228:3306/mybatis</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>root</value>
        </property>
    </bean>

       <!-- mybatis的sqlSession的工厂 sqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="jdbcDataSource" />
        <property name="typeAliasesPackage" value="com.lky.model"/>
    </bean>

       <!-- mybatis自动扫描加载sql映射文件、接口:MapperScannerConfigurer
         basePackage:指定sql映射文件,接口所在的包(自动扫描)
         sqlSessionFactory: 引用上面定义的sqlSessionFactory
       -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lky.dao"/>
        <property name="sqlSessionFactory"  ref="sqlSessionFactory"/>
    </bean>

        <!-- 事务管理  DataSourceTransactionManager
            dataSource:引用上面定义的数据源
         -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource"  ref="jdbcDataSource"/>
    </bean>

       <!-- 使用声明的事务
               使用上面定义的事务
        -->
    <tx:annotation-driven transaction-manager="txManager"/>
</beans>

4.实体类

package com.lky.model;

import java.util.Date;

public class User {
    private int id;
    private String name;
    private Date birthday;
    private double salary;
    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 Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    public User(int id, String name, Date birthday, double salary) {
        super();
        this.id = id;
        this.name = name;
        this.birthday = birthday;
        this.salary = salary;
    }
    public User() {
        super();
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", birthday=" + birthday
                + ", salary=" + salary + "]";
    }

}

5.接口类

package com.lky.dao;

import java.util.List;
import com.lky.model.User;

public interface UserMapper {
    public void add(User user);
    public void update(User user);
    public void delete(User user);
    public void findById(int id);
    public  List<User> findAll();

}

6.测试文件

package com.lky.test;

import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.lky.dao.UserMapper;
import com.lky.model.User;

@RunWith(SpringJUnit4ClassRunner.class)//使用Spring的测试框架
@ContextConfiguration("/beans.xml")//加载spring的配置文件bean
public class smTest {

    @Autowired //自动注入该对象
    private UserMapper userMapper;

    @Test
    public void testAdd(){
        User user=new User(-1,"tom",new Date(),12345);
        userMapper.add(user);

    }

    @Test
    public void testfindByName(){
        System.out.println(userMapper.findByName("tom"));

    }

    @Test
    public void testfingAll(){
        List<User> ss=userMapper.findAll();
        System.out.println(ss);
    }
}
时间: 2024-08-11 10:48:52

Spring+Mybatis+mysql配置的相关文章

springmvc+spring+mybatis+mysql配置过程

环境:eclipse 项目目录: jar包: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http:/

SpringMVC +Spring + MyBatis + Mysql + Redis(作为二级缓存) 配置

转载:http://blog.csdn.net/xiadi934/article/details/50786293 项目环境: 在SpringMVC +Spring + MyBatis + MySQL.Redis部署在Linux虚拟机. 1.整体思路 参考Ehcache实现MyBatis二级缓存代码(Maven引用对应jar查阅) 使用Spring管理Redis连接池 模仿EhcacheCache,实现RedisCache 2.pom.xml中加入Maven依赖 1 <!-- spring-re

springmvc学习总结(二) -- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解

@[email protected] 写在最前 之前分享过下面这几篇: mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上)(附demo和搭建过程遇到的问题解决方法) mybatis学习笔记(六) -- maven+spring+mybatis从零开始搭建整合详细过程(下) springmvc学习笔记(一) -- 从零搭建,基础入门 这一篇,在这些练习的基础上,将它们整合在一起! 搭建步骤如下 一.新建maven项目,配置环境,测试是否配置成

SpringMVC+Spring+Mybatis+Mysql项目搭建

眼下俺在搭建一个自己的个人站点玩玩.一边练习.一边把用到的技术总结一下,日后好复习. 站点框架大致例如以下图所看到的: 眼下仅仅用到了SpringMVC+Spring+Mybatis+Mysql.把它弄到了自己的server上来玩耍. 后台结构图: 眼下主要分为: view–controller层与前台交互,登陆拦截器 utils–工具类.一些经常使用的工具类. interceptor–拦截器.页面请求地址拦截和预防XSS漏洞拦截. impl–接口类,Service层-进行业务处理. excep

freemarker + spring mvc + spring + mybatis + mysql + maven项目搭建

今天说说搭建项目,使用freemarker + spring mvc + spring + mybatis + mysql + maven搭建web项目. 先假设您已经配置好eclipse的maven,创建好一个maven的web项目--Demo.我这里是jdk1.7,tomcat7. 修改pom.xml如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/

Ajax+SpringMVC+Spring+Mybatis+MySql+js用户注册实例

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:这几天研究了下Ajax注册的方法,通过在注册时输入用户名或邮箱等,就可以判断这个用户是否存在,以免用户来注册,然后提交了,系统才提示该用户名或邮箱不可用.使用Ajax便可实现这一功能,看了网上的都是php的,想想索性来写一个SpringMVC+Spring+Mybatis的.文章内容用到了很多技术,包括javascript.jquery.json.e表达式等. 先来看看最终效果: 注册

在spring,mybatis整合配置中走的弯路(1)

在接触一个新东西,总免不了走一些弯路,也正是在这些弯路中,我们不断的成长. 从git上把之前写的代码扒下来,看看我在当初使用spring与mybatis中所走的弯路,路过的君子也可引以为戒. <!-- 事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name

SpringMVC+Spring+Mybatis整合配置

1.Maven依赖文件:pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&g

Spring+Mybatis+MySql+Maven 简单的事务管理案例

利用Maven来管理项目中的JAR包,同时使用Spring在业务处理层进行事务管理.数据库使用MySq,数据处理层使用Spring和Mybatis结合. 本案例代码主要结构如图: 1.数据库脚本 -- ---------------------------- -- Table structure for `user` -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` (   `id`