Spring+Mybatis+mysql配置
- 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);
}
}
Spring+Mybatis+mysql配置的更多相关文章
- SpringMVC+Spring+mybatis项目从零开始--Spring mybatis mysql配置实现
上一章我们把SSM项目结构已搭建(SSM框架web项目从零开始--分布式项目结构搭建)完毕,本章将实现Spring,mybatis,mysql等相关配置. 1. 外部架包依赖引入 外部依赖包引入 ...
- SpringMVC +Spring + MyBatis + Mysql + Redis(作为二级缓存) 配置
转载:http://blog.csdn.net/xiadi934/article/details/50786293 项目环境: 在SpringMVC +Spring + MyBatis + MySQL ...
- freemarker + spring mvc + spring + mybatis + mysql + maven项目搭建
今天说说搭建项目,使用freemarker + spring mvc + spring + mybatis + mysql + maven搭建web项目. 先假设您已经配置好eclipse的maven ...
- springmvc学习总结(二) -- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解
@_@ 写在最前 之前分享过下面这几篇: mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上)(附demo和搭建过程遇到的问题解决方法) myba ...
- Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建
目录 Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建 0.项目准备 1.数据持久层Mybatis+MySQL 1.1 MySQL数据准备 1.2 Mybatis ...
- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解
转自:https://www.cnblogs.com/lmei/p/7190755.html?utm_source=itdadao&utm_medium=referral @_@ 写在最前 之 ...
- SpringMVC+Spring+Mybatis+Mysql项目搭建
眼下俺在搭建一个自己的个人站点玩玩.一边练习.一边把用到的技术总结一下,日后好复习. 站点框架大致例如以下图所看到的: 眼下仅仅用到了SpringMVC+Spring+Mybatis+Mysql.把它 ...
- 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+SpringMVC项目详解
http://blog.csdn.net/noaman_wgs/article/details/53893948 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+Spri ...
- SpringMVC+Mybatis+MySQL配置Redis缓存
SpringMVC+Mybatis+MySQL配置Redis缓存 1.准备环境: SpringMVC:spring-framework-4.3.5.RELEASE-dist Mybatis:3.4.2 ...
随机推荐
- C++之static
一.静态全局变量和非静态全局变量 1. 隐藏作用 比较非静态全局变量和静态(static)全局变量: 对于多个文件的代码,非静态全局变量和函数都是全局可见的.举例如下: a.c中: #include& ...
- nosqlunit开源框架
import com.lordofthejars.nosqlunit.annotation.UsingDataSet;import com.lordofthejars.nosqlunit.core.L ...
- sql Server 常用存储过程的优化
优化存储过程有很多种方法,下面介绍最常用的7种. 1.使用SET NOCOUNT ON选项 我们使用SELECT语句时,除了返回对应的结果集外,还会返回相应的影响行数.使用SET NOCOUNT ON ...
- 关于华为交换机bpdu enable. ntdp enable. ndp enable解析
华为5300初始状态下每个口子都有,bpdu enable. ntdp enable. ndp enable.不是很明白什么意思,有什么样的用途. BPDU是网桥协议数据单元(Bridge Proto ...
- oracle登陆连接的问题
一.登陆 1.使用客户端 直接在database中配置: IP:1521/orcl 其中IP为要连接的IP 其中1521为要连接的数据库的端口 其中orcl为要连接的数据库的实例名字 2.使用命令行 ...
- UIWebView加载不了页面, 但在电脑的浏览器上可以打开
经排查, 系因为URL中包含有中文, 所以无法加载页面, 解决方法如下: 将URL进行转码 NSString *urlStr =[[NSString stringWithFormat:@"h ...
- Php GMP
GMP是The GNU MP Bignum Library,是一个开源的数学运算库,它可以用于任意精度的数学运算,包括有符号整数.有理数和浮点数.它本身并没有精度限制,只取决于机器的硬件情况. 本函数 ...
- ionic开发环境搭建
Advanced HTML5 mobile development framework and SDK. Build incredible mobile apps with web technolog ...
- Qt5-控件-QMenu,QMenuBar-菜单栏详解-菜单热键-菜单校验功能
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QMenu> #inclu ...
- Qt5如何设置静态编译,解决生成的可执行文件打开出错问题
将https://yunpan.cn/cqGGURjmG2fEY 访问密码 8de5 中的压缩包Qt5-MSVC-Static-master.zip 解压到你的qt安装目录,一般就是C:\Qt下, ...