转载请注明出处:https://my.csdn.net/
上一篇有新建一个springboot项目,建好以后启动说是会自己生成一个application.xml和application.yaml。不知道啥原因我的没生成,自己手动建的,还有templates文件夹,如图:
1,配置文件properties和yaml
application.xml
# application.properties
# Server settings (ServerProperties)
server.port=8080
server.address=127.0.0.1
#server.sessionTimeout=30
server.contextPath=/
# Tomcat specifics
#server.tomcat.accessLogEnabled=false
server.tomcat.protocolHeader=x-forwarded-proto
server.tomcat.remoteIpHeader=x-forwarded-for
server.tomcat.basedir=
server.tomcat.backgroundProcessorDelay=30
server.port=8080,设置端口号,访问地址变成http://localhost:8080/
server.address=127.0.0.1,访问主机地址
#server.sessionTimeout=30,session失效时间
server.contextPath=/ ,访问路径,若你设置成 server.contextPath=/ test,则访问路径变为http://localhost:8080/test/
application.yaml
# application.yaml
# Server settings (ServerProperties)
server:
port: 8080
address: 127.0.0.1
sessionTimeout: 30
contextPath: /aaa
# Tomcat specifics
tomcat:
accessLogEnabled: false
protocolHeader: x-forwarded-proto
remoteIpHeader: x-forwarded-for
basedir:
backgroundProcessorDelay: 30 # secs
我用的是application.yml
(提示:)
官方建议application.java放的位置:
最后尝试了下修改下Application上的注解,我本来复制官方的代码用的是@Controller和@EnableAutoConfiguration,试着换成了@SpringBootApplication注解,出乎意外的可以扫描到Controller
又查了下官方的文档终于找到原因了,原因是:
如果使用@Controller和@EnableAutoConfiguration 注解还应该再加上一个注解:@ComponentScan 就可以了。@Controller和@EnableAutoConfiguration没有扫描注解的功能,而@ComponentScan是
springboot专门用来扫描@Component, @Service, @Repository, @Controller等注解的注解
总结:
使用springboot启动类配置扫描的两种注解配置方式:
1、@Controller
@EnableAutoConfiguration
@ComponentScan
2、@SpringBootApplication
@SpringBootApplication注解等价于@Configuration, @EnableAutoConfiguration and @ComponentScan
另外application.java(启动类)也应该按照官方的建议放在root目录下,这样才能扫描到Service和dao,不然还会引起,扫描不到注解的问题。
结合mybatis的配置
application.yml
# application.yaml
# Server settings (ServerProperties)
server:
port: 8088
address: 127.0.0.1
sessionTimeout: 30
contextPath: /aaa
# Tomcat specifics
tomcat:
accessLogEnabled: false
protocolHeader: x-forwarded-proto
remoteIpHeader: x-forwarded-for
basedir:
backgroundProcessorDelay: 30 # secs
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/government
username: root
password: Cx@520520
mybatis:
type-aliases-package: cn.cx.pro.bean
这里要注意mybatis的 type-aliases-package 改成自己的bean包
这是没有配置版的。
UserMapper.class
/**
*
*/
/**
* @author chenqincheng
*
*/
package cn.cx.pro.mapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import cn.cx.pro.bean.User;
import java.util.List;
// @Mapper 这里可以使用@Mapper注解,但是每个mapper都加注解比较麻烦,所以统一配置@MapperScan在扫描路径在application类中
public interface UserMapper {
@Select("SELECT * FROM tb_user WHERE id = #{id}")
User getUserById(Integer id);
@Select("SELECT * FROM egov_user")
public List<User> getUserList();
@Insert("insert into tb_user(username, age, ctm) values(#{username}, #{age}, now())")
public int add(User user);
@Update("UPDATE tb_user SET username = #{user.username} , age = #{user.age} WHERE id = #{id}")
public int update(@Param("id") Integer id, @Param("user") User user);
@Delete("DELETE from tb_user where id = #{id} ")
public int delete(Integer id);
}
UserServiceImpl.class
/**
*
*/
/**
* @author chenqincheng
*
*/
package cn.cx.pro.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.cx.pro.bean.User;
import cn.cx.pro.mapper.UserMapper;
import cn.cx.pro.service.UserService;
import java.util.List;
/**
* @ClassName
* @Description
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
public User getUserById(Integer id) {
return userMapper.getUserById(id);
}
public List<User> getUserList() {
return userMapper.getUserList();
}
public int add(User user) {
return userMapper.add(user);
}
public int update(Integer id, User user) {
return userMapper.update(id, user);
}
public int delete(Integer id) {
return userMapper.delete(id);
}
}
UserService.class
/**
*
*/
/**
* @author chenqincheng
*
*/
package cn.cx.pro.service;
import org.springframework.stereotype.Service;
import cn.cx.pro.bean.User;
import java.util.List;
/**
* @ClassName
* @Description
*/
public interface UserService {
User getUserById(Integer id);
public List<User> getUserList();
public int add(User user);
public int update(Integer id, User user);
public int delete(Integer id);
}
JsonResult.class
package cn.cx.pro.bean;
public class JsonResult {
private String status = null;
private Object result = null;
public JsonResult status(String status) {
this.status = status;
return this;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
// Getter Setter
}
MainApplication.class
package cn.cx.pro;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("cn.cx.pro.mapper")
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
这里注意@MappScan里面是扫描的mapper,也就是你的接口的包路径。
现在访问http://localhost:8088/aaa/users 就能出现你的东西了,前提是你的mysql也有这张表呢
有配置版的
修改如下几个配置就可以
application.yml
# application.yaml
# Server settings (ServerProperties)
server:
port: 8088
address: 127.0.0.1
sessionTimeout: 30
contextPath: /aaa
# Tomcat specifics
tomcat:
accessLogEnabled: false
protocolHeader: x-forwarded-proto
remoteIpHeader: x-forwarded-for
basedir:
backgroundProcessorDelay: 30 # secs
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/government
username: root
password: Cx@520520
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
config-location: classpath:mybatis/mybatis-config.xml
加入下面的
注意你的路径maper路径和config-location路径
UserMapper.class
/**
*
*/
/**
* @author chenqincheng
*
*/
package cn.cx.pro.mapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import cn.cx.pro.bean.User;
import java.util.List;
/**
* @ClassName
* @Description
*/
@Repository
public interface UserMapper {
User getUserById(Integer id);
public List<User> getUserList();
public int add(User user);
public int update(@Param("id") Integer id, @Param("user") User user);
public int delete(Integer id);
}
建立如图的mybatis xml配置文件
userMapper.xml(注意你的mapper对应的namespace和实体类对于的type路径)
<?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="cn.cx.pro.mapper.UserMapper" >
<resultMap id="BaseResultMap" type="cn.cx.pro.bean.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="user_name" property="user_name" jdbcType="VARCHAR" />
<!-- <result column="age" property="age" jdbcType="INTEGER" />
<result column="ctm" property="ctm" jdbcType="TIMESTAMP"/> -->
</resultMap>
<sql id="Base_Column_List" >
id, user_name
</sql>
<select id="getUserList" resultMap="BaseResultMap" >
SELECT
<include refid="Base_Column_List" />
FROM egov_user
</select>
<select id="getUserById" parameterType="java.lang.Integer" resultMap="BaseResultMap" >
SELECT
<include refid="Base_Column_List" />
FROM tb_user
WHERE id = #{id}
</select>
<insert id="add" parameterType="cn.cx.pro.bean.User" >
INSERT INTO
tb_user
(username,age,ctm)
VALUES
(#{username}, #{age}, now())
</insert>
<update id="update" parameterType="java.util.Map" >
UPDATE
tb_user
SET
username = #{user.username},age = #{user.age}
WHERE
id = #{id}
</update>
<delete id="delete" parameterType="java.lang.Integer" >
DELETE FROM
tb_user
WHERE
id = #{id}
</delete>
</mapper>
mybatis-config.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>
<typeAliases>
</typeAliases>
</configuration>
现在运行: http://localhost:8088/aaa/users
可出现
有什么问题可以加我weixin互相学习呢