使用idea 搭建Spring+mybatis
1、file-new-project
项目的结构如下:
在WEB-INF 下面新建一个 文件夹lib
右键WEB-INF ,new-Directory
所需要的jar 包有:
lib下载地址:
网盘地址:https://pan.baidu.com/s/1YmDtsuhTQQp-HrZB3affGQ 提取码:26bs
然后右键 lib文件夹,选择Add as Library..
点击Ok,如下:
右键项目,new-Directory,
然后,右键刚刚新建的文件夹config,选择 Make Directory as..--->Sources Root
然后,在config 文件夹下面新建三个文件夹,分别是:mybatis,spring,sqlmapperk,并创建三个空的文件夹,如下图所示:
然后,在src 下面新建三个文件夹,po(用于放实体类),test(用于单元测试),dao,dao.impl
如图所示:
在config文件夹下面新建一个db.properties 和 log4j.properties
到这里,文件夹就弄好了.
下面进行开发内容:
1、config/mybatis/SqlMapConfig.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>
<mapper resource="sqlmapper/UserMapper.xml"/>
</mappers>
</configuration>
2、config/spring/applicationContext.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
"
> <!--加载配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<context:property-placeholder location="classpath:log4j.properties"/> <!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="5"/> </bean>
<!--配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--加载SqlMapConfig.xml配置文件-->
<property name="configLocation" value="mybatis/SqlMapConfig.xml"/>
<!--数据源-->
<property name="dataSource" ref="dataSource"/>
</bean> <!--注入UserInfoDaoImpl 接口实现类-->
<bean id="userInfoDao" class="com.wlc.dao.impl.UserMapperDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
3、编写UserMapper.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="test">
<select id="findUserById" parameterType="java.lang.Integer" resultType="com.wlc.po.UserInfo">
select * from userInfo where id =#{id}
</select>
</mapper>
4、编写jdbc.properties
jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=scott
jdbc.password=tiger
5、log4j.properties
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n #文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File = logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
6、编写UserMapperDao 接口
package com.wlc.dao; import com.wlc.po.UserInfo; /**
* @author 王立朝
* @version 1.0
* @description com.wlc.dao
* @date 2019/1/25
**/
public interface UserMapperDao {
//根据用户id查看用户信息
UserInfo findUserById(Integer integer);
}
7、编写UserMapperDaoImpl.java
package com.wlc.dao.impl; import com.wlc.dao.UserMapperDao;
import com.wlc.po.UserInfo;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport; /**
* @author 王立朝
* @version 1.0
* @description com.wlc.dao.impl
* @date 2019/1/25
**/
public class UserMapperDaoImpl extends SqlSessionDaoSupport implements UserMapperDao { @Override
public UserInfo findUserById(Integer integer) {
SqlSession sqlSession = this.getSqlSession();
//sqlSession.selectOne("namespace.Sql方法的id");
UserInfo userInfo = sqlSession.selectOne("test.findUserById");
return userInfo;
}
}
8、编写UserInfo.java 实体类
package com.wlc.po; /**
* @author 王立朝
* @version 1.0
* @description po
* @date 2019/1/16
**/
public class UserInfo {
//用户id
private Integer id;
//用户名
private String userName;
//密码
private String passWord;
//性别
private String sex;
//邮箱
private String email;
//联系方式
private String phone;
//家庭住址
private String address;
//年龄
private Integer age;
//血型
private String bloodType; public UserInfo(Integer id, String userName, String passWord, String sex, String email, String phone, String address, Integer age, String bloodType) {
this.id = id;
this.userName = userName;
this.passWord = passWord;
this.sex = sex;
this.email = email;
this.phone = phone;
this.address = address;
this.age = age;
this.bloodType = bloodType;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassWord() {
return passWord;
} public void setPassWord(String passWord) {
this.passWord = passWord;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getBloodType() {
return bloodType;
} public void setBloodType(String bloodType) {
this.bloodType = bloodType;
} public UserInfo() {
} @Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
", sex='" + sex + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
", age=" + age +
", bloodType='" + bloodType + '\'' +
'}';
}
}
9、编写测试类
UserMapperTest.java
package com.wlc.test; import com.wlc.dao.UserMapperDao;
import com.wlc.po.UserInfo;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author 王立朝
* @version 1.0
* @description com.wlc.test
* @date 2019/1/25
**/ public class UserMapperTest {
private ApplicationContext applicationContext; @Before
//@Before :执行单元测试方法之前会先执行下面这个方法
public void doBefore() {
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
System.out.println("可以到达");
} @Test
//测试UserMapper
public void testUserMapper() { UserMapperDao userMapperDao = (UserMapperDao) applicationContext.getBean("userMapperDao");
UserInfo userInfo = userMapperDao.findUserById(2);
System.out.println("用户的信息为:"+"性别:"+userInfo.getUserName()
+"年龄:"+ userInfo.getAge());
} }
结果如下:
使用idea 搭建Spring+mybatis的更多相关文章
- 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+SpringMVC项目详解
http://blog.csdn.net/noaman_wgs/article/details/53893948 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+Spri ...
- 搭建spring+mybatis+struts2环境的配置文件
1.web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=& ...
- web项目环境搭建(3):搭建Spring+MyBatis
spring 4.1.7 + MyBatis 3.3 正式开始配置关键内容,这是硬货 一. 新建spring配置文件,起名为 applicationContext.xml,放在src/main/res ...
- 搭建Spring+mybatis报错
java.lang.ClassCastException: com.sun.proxy.$Proxy12 cannot be cast to com.bdqn.service.impl.UserSer ...
- Spring+Mybatis+Maven+MySql搭建实例
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何使用Maven来搭建Spring+Mybatis+MySql的的搭建实例 ...
- Spring+Mybatis+SpringMVC+Maven+MySql搭建实例
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何使用Maven来搭建Spring+Mybatis+SpringMVC+M ...
- Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建
目录 Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建 0.项目准备 1.数据持久层Mybatis+MySQL 1.1 MySQL数据准备 1.2 Mybatis ...
- 【转】Spring+Mybatis+SpringMVC+Maven+MySql搭建实例
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何使用Maven来搭建Spring+Mybatis+SpringMVC+M ...
- 手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)
手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版) SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级架构方案.标准的MVC设计模式, ...
随机推荐
- VS2015编译GDAL2.2.1源码
下载完GDAL2.2.1,你会发现这货没有CMakeLists.txt,对于我这样的只会用CMake GUI的货来说,着实很难过. 需要用VS带的nmake来生成.sln 管理员身份启动VS2015命 ...
- php学习九:存取器&魔术方法
当声明属性的时候用到了private,那么的话只能本类中才能访问此属性,其他地方无法使用,这个时候需要给外界提供一些方法来使外界访问 方法一:存取器: 可以提供一个public的方法来使外界对类里面的 ...
- 点击一个textView里的link导航至程序内可返回的自定义webView
1,在AppDelegate.h里定义一个 id currentViewController; 在AppDelegate.m里 @implementation UIApplication (Priva ...
- 计算时间:一个C++运算符重载示例
Time类是一个用于计算时间的类,其原型如下:程序清单11.1 mytime0.h // mytime0.h -- Time class before operator overloading #if ...
- 使用Android Studio调试内存问题
http://blog.csdn.net/yutao52shi/article/details/50055669 前言 内存问题对于Android开发者是永远的痛.如果一个android程序员说他没有 ...
- c# Socket通信基础
一.IP地址操作类 1.IPAddress类 a.在该类中有一个 Parse()方法,可以把点分的十进制IP表示转化成IPAddress类,方法如下: IPAddress address = I ...
- mybatis的<choose>和<when>、<otherwise>标签
SELECT<choose> <when test='timeType=="yy"'> TO_CHAR(REPORT_TIME,'yyyy') </w ...
- oracle的with as用法
转自:https://www.cnblogs.com/linjiqin/archive/2013/06/24/3152667.html with as语法–针对一个别名with tmp as (sel ...
- [MongoDB] 安装MongoDB配置Replica Set
MongoDB的环境主要包括StandAlone,Replication和Sharding. StandAlone:单机环境,一般开发测试的时候用. Replication:主从结构,一个Primar ...
- D2 Magic Powder -1/- 2---cf#350D2(二分)
题目链接:http://codeforces.com/contest/670/problem/D2 This problem is given in two versions that differ ...