springmvc+spring+mybatis实现用户登录功能(上)

网友投稿 229 2023-04-28


springmvc+spring+mybatis实现用户登录功能(上)

由于本人愚钝,整合ssm框架真是费劲了全身的力气,所以打算写下这篇文章,一来是对整个过程进行一个回顾,二来是方便有像我一样的笨鸟看过这篇文章后对其有所帮助,如果本文中有不对的地方,也请大神们指教。

一、代码结构

整个项目的代码结构如图所示:

controller为控制层,主要用于对业务模块的流程控制。

dao为数据接入层,主要用于与数据库进行连接,访问数据库进行操作,这里定义了各种操作数据库的接口。

mapper中存放mybatis的数据库映射配置。可以通过查看mybatis相关教程了解

model中存放了我们的实体类

service为业务层,我们的各种业务都定义在此,由controller调用不同业务实现不同的操作。

由于之前搭建环境都是自己配置依赖环境,导致缺各种缺包或者依赖冲突,所以这次我使用了maven来管理项目,可以上网查一下相关的教程,使用起来非常方便。

下面是我的pom.xml文件的配置

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com.mjl

testssm

war

1.0-SNAPSHOT

testssm Maven Webapp

http://maven.apache.org

UTF-8

4.1.4.RELEASE

2.5.0

junit

junit

4.12

test

org.springframework

spring-core

${spring.version}

org.springframework

spring-beans

${spring.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-tx

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-test

${spring.version}

test

org.mybatis

mybatis

3.2.8

org.mybatis

mybatis-spring

1.2.2

mysql

mysql-connector-java

5.1.34

com.mchange

c3p0

0.9.5-pre8

org.aspectj

aspectjweaver

1.8.4

log4j

log4j

1.2.17

javax.servlet

servlet-api

3.0-alpha-1

javax.servlet

jstl

1.2

org.codehaus.jackson

jackson-mapper-asl

1.9.13

commons-io

commons-io

2.4

commons-fileupload

commons-fileupload

1.2.2

testssm

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com.mjl

testssm

war

1.0-SNAPSHOT

testssm Maven Webapp

http://maven.apache.org

UTF-8

4.1.4.RELEASE

2.5.0

junit

junit

4.12

test

org.springframework

spring-core

${spring.version}

org.springframework

spring-beans

${spring.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-tx

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-test

${spring.version}

test

org.mybatis

mybatis

3.2.8

org.mybatis

mybatis-spring

1.2.2

mysql

mysql-connector-java

5.1.34

com.mchange

c3p0

0.9.5-pre8

org.aspectj

aspectjweaver

1.8.4

log4j

log4j

1.2.17

javax.servlet

servlet-api

3.0-alpha-1

javax.servlet

jstl

1.2

org.codehaus.jackson

jackson-mapper-asl

1.9.13

commons-io

commons-io

2.4

commons-fileupload

commons-fileupload

1.2.2

testssm

下面进入主题,直接上代码,我之前是先看了各种原理,但是看了半天也没明白,所以大家可以先把项目跑起来,然后再去对照springmvc的各个模块进行分析,哪个模块执行哪个操作,我认为这样的效果比较好。

我个人喜欢先从数据库与ORM框架与spirng开始搭建,这样的话在一开始就可以对ORM框架进行检验,免得在最后检验的时候出了问题。

先看web.xml的配置

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

contextConfigLocation

classpath*:config/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter&ltAALgrT;/filter-class>

encoding

UTF-8

forceEncoding

true

encodingFilter

/*

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

contextConfigLocation

classpath*:config/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter&ltAALgrT;/filter-class>

encoding

UTF-8

forceEncoding

true

encodingFilter

/*

配置完web.xml后,配置spring的applicationContext.xml,它是spring的配置文件,一般与spring集成的框架都要在这里进行配置。

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

<!-- 获取连接最大等待时间 -->

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

<!-- 获取连接最大等待时间 -->

在这里使用了jdbc.properties来分散配置,jdbc.properties中保存了数据库的信息,需要根据你们的数据库配置进行修改。

jdbc.properties

jdbc_driverClassName=com.mysql.jdbc.Driver

 jdbc_url=jdbc:mysql://localhost:3307/db_ssm?useUnicode=true&characterEncoding=utf-8

 jdbc_username=root

 jdbc_password=1234

然后是争对mybatis进行配置,由于我把数据库配置都配置在applicationcontext.xml中,所以我对mybatis中只进行了别名配置。

config.xml

"http://mybatis.org/dtd/mybatis-3-config.dtd">

配置完后,就开始进行数据库开发了,由于是一个简单的登录功能的实现,所以数据库也非常简单。

create database if not exists db_ssm character set utf8;

use db_ssm;

create table tb_user(

id int(10) auto_increment,

username varchar(20) not null,

password varchar(20) not null,

primary key (id)

)ENGINE=InnoDB DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

insert into tb_user(id,username,password) values(1,'alvin',1234);

创建完数据库后开始写model下面的实体类

User.java

package com.mjl.model;

/**

* Created by alvin on 15/9/7.

*/

public class User {

private int id;

private String username;

private String password;

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

}

数据库接入层dao的接口,在这里我使用了mybatis以接口方式编程,这部分借鉴了其他的教程,有不懂的地方可以查看http://yihaomen.com/article/java/302.htm 该网址的mybatis教程,我觉得写的很不错。

package com.mjl.dao;

import com.mjl.model.User;

/**

* Created by alvin on 15/9/7.

* 此类为接口模式下的配置

*/

public interface IUserDao {

//这里以接口形式定义了数据库操作方法,我们只需

// 在Mybatis映射文件中对其进行映射就可以直接使用

public User selectById(int id);

public User selectByName(String username);

}

mybatis映射文件文件名必须与接口类相同,否则无法映射成功。

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from tb_user where username = #{username}

select * from tb_user where id = #{id}

好了,到这里mybatis与spring已经整合完毕,我们需要测试一下mybatis是否与spring整合成功,写一个test类

package com.mjl.test;

import com.mjl.dao.IUserDao;

import com.mjl.model.User;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

* Created by Alvin on 15/9/6.

*/

public class Test {

private static ApplicationContext ac;

static {

ac = new ClassPathXmlApplicationContext("config/applicationContext.xml");

}

public static void main(String[] args) {

IUserDao mapper = (IUserDao) ac.getBean("IUserDao");

System.out.println("获取alvin");

User user = mapper.selectByName("alvin");

System.out.println(user.getId()+":"+"username:"+user.getUsername());

System.out.println("password:"+user.getPassword());

}

}

如果成功,如下图所示:

到这里mybatis与spring就整合结束了,明天继续更新下半部分。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:MyBatis接口的简单实现原理分析
下一篇:vue实现表格数据的增删改查
相关文章

 发表评论

暂时没有评论,来抢沙发吧~