不勾选任何值,直接next
根模块创建成功如下:
src目录不需要可以删除
src目录不需要可以删除
添加一个web模块
Facets->web->javaweb->OK
Artiffacts->web Application:EXploded->From Modules->javaweb->OK
可以看到javaweb模块下多了一个web目录
4.0.0
cn.edu.zut.ssm
JavaSSM
pom
1.0-SNAPSHOT
javadao
javaservice
javaweb
UTF-8
4.0.6.RELEASE
5.1.30
3.2.7
1.2.2
1.2.12
1.2
1.6.12
1.4
org.apache.maven.plugins
maven-compiler-plugin
3.7.0
1.8
junit
junit
3.8.1
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-test
${spring.version}
org.aspectj
aspectjweaver
${aspectj.version}
org.aspectj
aspectjrt
${aspectj.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
${mybatis.spring.version}
mysql
mysql-connector-java
${mysql.connector.version}
commons-dbcp
commons-dbcp
${commons-dbcp.version}
javax.servlet
jstl
${jstl.version}
javax.servlet
javax.servlet-api
3.1.0
JavaSSM
cn.edu.zut.ssm
1.0-SNAPSHOT
4.0.0
javaservice
jar
cn.edu.zut.ssm
javadao
1.0-SNAPSHOT
junit
junit
4.12
test
javadao层模块配置如下
JavaSSM
cn.edu.zut.ssm
1.0-SNAPSHOT
4.0.0
javadao
jar
JavaSSM
cn.edu.zut.ssm
1.0-SNAPSHOT
4.0.0
javaweb
war
cn.edu.zut.ssm
javadao
1.0-SNAPSHOT
cn.edu.zut.ssm
javaservice
1.0-SNAPSHOT
数据库的连接
view->tool windows->database->加号->data Source->mysql
填写Database:数据库名
root:用户名
password:密码
1.创建与数据库表对应的实体类
dome.java
package com.smart.pojo;
public class Dome {
private String userName;
private String password;
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;
}
@Override
public String toString() {
return "dome{" +
"userName='" + userName + '\'' +
", password='" + password + '\'' +
'}';
}
}
2.创建接口
DomeMapper.java
接口创建了两个方法名
用于插入和查询
package com.smart.dao;
import com.smart.pojo.Dome;
import java.util.List;
public interface DomeMapper {
List selectAll();
int insertdome(Dome dome);
}
3.创建映射文件
DomeMapper.xml
insert into t_dome(t_username, t_password) VALUES (#{userName,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR})
4.创建mybatis-config.xml配置文件
里面可以不写内容,但一定要有。
mybatis与spring整合后配置信息全都写在了spring的配置文件中
mybatis配置文件只做一些别名配置
spring-mybatis.xml文件配置如下:
在service 层下的Test文件夹下创建DomeTest.java
package com.smart.test;
import com.smart.dao.DomeMapper;
import com.smart.pojo.Dome;
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 java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-mybatis.xml")
public class DometTest {
@Autowired
private DomeMapper domeMapper;
@Test
public void InsertDomeTest(){
Dome dome=new Dome();
dome.setUserName("Test");
dome.setPassword("123456");
System.out.println(domeMapper.insertdome(dome));
}
@Test
public void SelectAllTest(){
List list=domeMapper.selectAll();
for(Object dome:list){
System.out.println(dome.toString());
}
}
}
1.首先测试插入操作,运行成功后查看数据库中是否新增数据
然后测试查询方法
如下结果即成功
文件目录如下
配置如下
xml version="1.0" encoding="UTF-8"?>version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> jsp/index.jsp contextConfigLocation classpath:spring-mybatis.xml encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 encodingFilter /* org.springframework.web.context.ContextLoaderListener org.springframework.web.context.request.RequestContextListener smart org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring-mvc.xml 1 smart *.html
代码如下
package com.smart.service;
import com.smart.dao.DomeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DomeService {
@Autowired
private DomeMapper domeMapper;
public List index(){
return domeMapper.selectAll();
}
}
2.在web包下创建DomeController.java
package com.smart.web;
import com.smart.service.DomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
public class DomeController {
@Autowired
private DomeService domeService;
@RequestMapping(value ="/index.html")
public ModelAndView index(){
List list=domeService.index();
ModelAndView mav =new ModelAndView();
mav.setViewName("index");
mav.addObject("userList",list);
return mav;
}
}
3.jsp文件夹下创建index.jsp文件
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/5/15 0015
Time: 22:52
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
Hello
获取表格数据
用户名
密码
${user.userName}
${user.password}