使用maven整合spring+springmvc+mybatis
2021-06-26 20:06
标签:character tis loader port release div resource VID err 在Maven dependencies中可以看到jar包,说明引入成功了 db.properties mybatis-config.xml applicationContext-dao.xml applicationContext-tx.xml springmvc.xml web.xml 配置完成后,写一个简单的用户登录操作: 然后启动tomcat: 使用maven整合spring+springmvc+mybatis 标签:character tis loader port release div resource VID err 原文地址:https://www.cnblogs.com/start-21/p/9655865.html使用maven整合spring+springmvc+mybatis
开发环境:
1. jdk1.8
2. eclipse4.7.0 (Oxygen)
3. mysql 5.7
在pom.xml文件中,引入我们需要的jar包:(按需引入jar包)
properties>
spring.version>4.3.18.RELEASEspring.version>
mybatis.version>3.4.5mybatis.version>
mybatis.spring.version>1.3.1mybatis.spring.version>
properties>
dependencies>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-webmvcartifactId>
version>${spring.version}version>
scope>compilescope>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-txartifactId>
version>${spring.version}version>
scope>compilescope>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-jdbcartifactId>
version>${spring.version}version>
scope>compilescope>
dependency>
dependency>
groupId>org.aspectjgroupId>
artifactId>aspectjweaverartifactId>
version>1.8.13version>
dependency>
dependency>
groupId>javax.servletgroupId>
artifactId>javax.servlet-apiartifactId>
version>4.0.1version>
scope>providedscope>
dependency>
dependency>
groupId>javax.servlet.jspgroupId>
artifactId>javax.servlet.jsp-apiartifactId>
version>2.3.1version>
scope>providedscope>
dependency>
dependency>
groupId>javax.servlet.jsp.jstlgroupId>
artifactId>javax.servlet.jsp.jstl-apiartifactId>
version>1.2.2version>
dependency>
dependency>
groupId>org.apache.taglibsgroupId>
artifactId>taglibs-standard-implartifactId>
version>1.2.5version>
dependency>
dependency>
groupId>org.mybatisgroupId>
artifactId>mybatisartifactId>
version>${mybatis.version}version>
scope>compilescope>
dependency>
dependency>
groupId>org.mybatisgroupId>
artifactId>mybatis-springartifactId>
version>${mybatis.spring.version}version>
scope>compilescope>
dependency>
dependency>
groupId>com.alibabagroupId>
artifactId>druidartifactId>
version>1.1.10version>
scope>compilescope>
dependency>
dependency>
groupId>mysqlgroupId>
artifactId>mysql-connector-javaartifactId>
version>5.1.46version>
dependency>
dependency>
groupId>log4jgroupId>
artifactId>log4jartifactId>
version>1.2.17version>
dependency>
dependencies>
定义目录结构:
配置文件:
jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/db_ssm
jdbc_username=root
jdbc_password=123456
settings>
setting name="logImpl" value="LOG4J"/>
setting name="lazyLoadingEnabled" value="true"/>
setting name="aggressiveLazyLoading" value="false"/>
setting name="lazyLoadTriggerMethods" value="clone"/>
settings>
typeAliases>
package name="com.login.dao"/>
typeAliases>
context:property-placeholder location="classpath:db.properties"/>
bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
property name="driverClassName" value="${jdbc_driverClassName}">property>
property name="url" value="${jdbc_url}">property>
property name="username" value="${jdbc_username}">property>
property name="password" value="${jdbc_password}">property>
bean>
bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
property name="dataSource" ref="dataSource">property>
property name="mapperLocations">
list>
value>classpath:mappers/*.xmlvalue>
list>
property>
property name="configLocation" value="classpath:mybatis/mybatis-config.xml">property>
bean>
bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
property name="basePackage" value="com.login.dao">property>
property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
bean>
context:component-scan base-package="com.login.service">context:component-scan>
bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
property name="dataSource" ref="dataSource">property>
bean>
tx:advice id="tx_advice" transaction-manager="tx" >
tx:attributes>
tx:method name="save*" propagation="REQUIRED"/>
tx:method name="add*" propagation="REQUIRED"/>
tx:method name="delete*" propagation="REQUIRED"/>
tx:method name="update*" propagation="REQUIRED"/>
tx:method name="do*" propagation="REQUIRED"/>
tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
tx:method name="list*" propagation="SUPPORTS" read-only="true"/>
tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
tx:method name="count*" propagation="SUPPORTS" read-only="true"/>
tx:attributes>
tx:advice>
aop:config proxy-target-class="false">
aop:advisor advice-ref="tx_advice" pointcut="execution(* com.login.service.*Service.*(..) )"/>
aop:config>
context:component-scan base-package="com.login.web.controller">context:component-scan>
mvc:annotation-driven>mvc:annotation-driven>
bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
property name="prefix" value="/">property>
property name="suffix" value=".jsp">property>
bean>
mvc:default-servlet-handler/>
filter>
filter-name>characterEncodingFilterfilter-name>
filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
init-param>
param-name>encodingparam-name>
param-value>UTF-8param-value>
init-param>
init-param>
param-name>forceRequestEncodingparam-name>
param-value>trueparam-value>
init-param>
init-param>
param-name>forceResponseEncodingparam-name>
param-value>trueparam-value>
init-param>
filter>
filter-mapping>
filter-name>characterEncodingFilterfilter-name>
url-pattern>/*url-pattern>
filter-mapping>
context-param>
param-name>contextConfigLocationparam-name>
param-value>classpath:spring/applicationContext-*.xmlparam-value>
context-param>
listener>
listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
servlet>
servlet-name>spring-mvcservlet-name>
servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
init-param>
param-name>contextConfigLocationparam-name>
param-value>classpath:spring/spring-mvc.xmlparam-value>
init-param>
load-on-startup>1load-on-startup>
servlet>
servlet-mapping>
servlet-name>spring-mvcservlet-name>
url-pattern>/url-pattern>
servlet-mapping>
@Controller
public class UserController {
@Autowired
private IUserService service;
@RequestMapping(path="/login")
public String login(String username,String password,Model m) {
User user = service.login(username, password);
m.addAttribute("user", user);
return "success";
}
}
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper mapper;
@Override
public User login(String username, String password) {
//参数验证是否为空
//参数判断--》账号,密码是否正确
return mapper.getUserByUsername(username);
}
}
mapper namespace="com.login.dao.UserMapper">
select id="getUserByUsername" resultType="com.login.entity.User">
SELECT * FROM user WHERE username = #{username}
select>
mapper>
总结: 使用maven可以让我们不用担心找不到jar包。maven同时也为我们规定好了一些目录结构,方便我们的开发,但是maven的强大远不如此。让我们一起来学习吧!希望以上这些可以帮到需要帮助的朋友!
上一篇:java对象创建过程简介
下一篇:一种多线程设计思路
文章标题:使用maven整合spring+springmvc+mybatis
文章链接:http://soscw.com/essay/98169.html