Idea,Mysql5.7,Tomcat 9,Maven 3.8,Lombok
项目搭建->MySQL->Mybatis->Spring->SpringMVC->web.xml
配置、连接、绑定、操作数据库
用于配置数据库连接的用户名和密码
Spring 整合配置文件
与dao层关联
与service层关联
与controller关联
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student_manager?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.18version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.46version>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.5.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.3.4version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.3.4version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.8.9version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.62version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
build>
DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true" />
<setting name="logImpl" value="LOG4J" />
settings>
<typeAliases>
<package name="com.cxy.pojo"/>
typeAliases>
<mappers>
<mapper resource="com/cxy/mapper/mapperXml/UsersMapper.xml"/>
<mapper resource="com/cxy/mapper/mapperXml/InstituteMapper.xml"/>
<mapper resource="com/cxy/mapper/mapperXml/DomainsMapper.xml"/>
mappers>
configuration>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:database.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<property name="autoCommitOnClose" value="false"/>
<property name="checkoutTimeout" value="10000"/>
<property name="acquireRetryAttempts" value="2"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.cxy.mapper"/>
bean>
beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="com.cxy.service" />
<bean id="usersServiceImpl" class="com.cxy.service.impl.UsersServiceImpl">
<property name="usersMapper" ref="usersMapper"/>
bean>
<bean id="domainsServiceImpl" class="com.cxy.service.impl.DomainsServiceImpl">
<property name="domainsMapper" ref="domainsMapper"/>
bean>
<bean id="instituteServiceImpl" class="com.cxy.service.impl.InstituteServiceImpl">
<property name="instituteMapper" ref="instituteMapper"/>
bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
bean>
beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven >
<mvc:message-converters>
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8value>
<value>application/json;charset=UTF-8value>
list>
property>
bean>
mvc:message-converters>
mvc:annotation-driven>
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
bean>
<context:component-scan base-package="com.cxy.controller" />
<mvc:resources location="/static/" mapping="/static/**"/>
beans>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
<import resource="spring-mvc.xml"/>
beans>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>DispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>DispatcherServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<filter>
<filter-name>encodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<session-config>
<session-timeout>15session-timeout>
session-config>
web-app>
#//注意这里不要用INFO,DEBUG等 改个名字
log4j.rootLogger=infoA,errorA,stdout,DEBUGA
#过滤掉spring框架下的额外日志
log4j.category.org.springframework = WARN
#输出到控制台
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.Threshold = debug
log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c{1}:%L - %m%n
#c3p0
log4j.logger.com.mchange.v2.async.ThreadPoolAsynchronousRunner = ERROR
#打印sql语句
log4j.logger.com.ibatis=DEBUG
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Users {
private int uId;
private String uName;
private int uAge;
private int uStatus;
private int uRole;
private String uPassword;
private int dId;
private int iId;
private String iName;
private String dName;
}
@Mapper
public interface UsersMapper {
//用户管理的时候需要查看所有用户
List<Users> selectAllUsers();
//用户的登陆的验证
Users selectUsersLogin(@Param("uName") String uName, @Param("uPassword") String uPassword);
//验证用户是否存在
int selectUserIsExit(@Param("uName") String uName);
//注册/新增一个新用户
int insertNewUser(@Param("users") Users users);
//连表查询的查询所有用户
List<Users> selectAllUsers1();
//删除某一个用户
int deleteOneUser(@Param("uId") int uId);
//获取未审核的所有用户
List<Users> selectNoPass();
//审核某一用户
int updateUser(@Param("uId") int uId);
//查询某一个用户
Users selectUserById(@Param("uId")int uId);
//修改用户信息
int upadateUserMsg(@Param("users")Users users);
}
<?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="com.cxy.mapper.UsersMapper">
<!--换成Users类的全限定名-->
<select id="selectAllUsers" resultType="com.cxy.pojo.Users">
select * from users
</select>
<select id="selectUsersLogin" resultType="Users" parameterType="java.lang.String">
select u_id,u_name,u_role,u_password,u_status,u_age,d_id,i_id from users where u_name=#{uName} and u_password=#{uPassword} and u_status=1;
</select>
<select id="selectUserIsExit" resultType="int" parameterType="Users">
select count(*) from users where u_name=#{uName};
</select>
<insert id="insertNewUser" parameterType="Users">
insert into users(u_name,u_age,u_password,d_id,i_id) values(#{users.uName},#{users.uAge},#{users.uPassword},#{users.dId},#{users.iId});
</insert>
<select id="selectAllUsers1" resultType="Users" >
select * from users as u,institute as i,domains as d
where u.i_id=i.i_id and u.d_id=d.d_id
</select>
<delete id="deleteOneUser" parameterType="int">
delete from users where u_id=#{uId}
</delete>
<select id="selectNoPass" resultType="Users">
select * from users where u_status=0;
</select>
<update id="updateUser" parameterType="int">
UPDATE users
SET u_status=1
WHERE u_id=#{uId};
</update>
<select id="selectUserById" parameterType="int" resultType="Users">
select * from users as u,institute as i,domains as d
where u.i_id=i.i_id and u.d_id=d.d_id and u.u_id=#{uId}
</select>
<update id="upadateUserMsg" parameterType="Users" >
update users set u_name=#{users.uName},u_age=#{users.uAge},u_status=#{users.uStatus},d_id=#{users.dId},i_id=#{users.iId} where u_id=#{users.uId}
</update>
</mapper>
public interface UsersService {
//用户管理的时候需要查看所有用户
List<Users> selectAllUsers();
//用户的登陆的验证
Users selectUsersLogin(String uName,String uPassword);
//验证用户是否存在
int selectUserIsExit(String uName);
//注册/新增一个新用户
int insertNewUser(Users users);
//连表查询的查询所有用户
List<Users> selectAllUsers1();
//删除某一个用户
int deleteOneUser(int uId);
//获取未审核的所有用户
List<Users> selectNoPass();
//审核某一用户
int updateUser(int uId);
//查询某一个用户
Users selectUserById(int uId);
//修改用户信息
int upadateUserMsg(Users users);
}
@Service
public class UsersServiceImpl implements UsersService {
private UsersMapper usersMapper;
public void setUsersMapper(UsersMapper usersMapper) {
this.usersMapper = usersMapper;
}
@Override
public List<Users> selectAllUsers() {
return usersMapper.selectAllUsers();
}
@Override
public Users selectUsersLogin(String uName, String uPassword) {
return usersMapper.selectUsersLogin(uName,uPassword);
}
@Override
public int selectUserIsExit(String uName) {
return usersMapper.selectUserIsExit(uName);
}
@Override
public int insertNewUser(Users users) {
return usersMapper.insertNewUser(users);
}
@Override
public List<Users> selectAllUsers1() {
return usersMapper.selectAllUsers1();
}
@Override
public int deleteOneUser(int uId) {
return usersMapper.deleteOneUser(uId);
}
@Override
public List<Users> selectNoPass() {
return usersMapper.selectNoPass();
}
@Override
public int updateUser(int uId) {
return usersMapper.updateUser(uId);
}
@Override
public Users selectUserById(int uId) {
return usersMapper.selectUserById(uId);
}
@Override
public int upadateUserMsg(Users users) {
return usersMapper.upadateUserMsg(users);
}
}
注意:
@Service注解注入bean要放在serviceImpl下面
public class TestNew {
@Test
public void test(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UsersServiceImpl usersService=(UsersServiceImpl) ctx.getBean("usersServiceImpl");
//DomainsServiceImpl domainsService=(DomainsServiceImpl) ctx.getBean("domainsServiceImpl");
Users users = usersService.selectUserById(2);
System.out.println(users);
}
}